SwiftUIVolumeButtonsHandler is simple way to handling clicks on hardware volume buttons on iPhone or iPad.
let volumeButtonHandler = VolumeButtonHandler(containerView: UIView())
.overlay(
VolumeButtonHandlerView(volumeButtonHandler: volumeButtonHandler)
)
.onAppear {
volumeButtonHandler.buttonClosure = { button in
switch button {
case .up:
print("Volume up button pressed")
// execute your code for volume up button press here
case .down:
print("Volume down button pressed")
// execute your code for volume down button press here
}
}
}
To install SwiftUIVolumeButtonsHandler, download the code as a zip file, then drag Log.swift
and VolumeButtonHandler.swift
into your project.
This project based on Objective-C code JPSVolumeButtonHandler