Cleanest way of capturing volume up/down button press on iOS 8

Ricardo Sanchez-Saez picture Ricardo Sanchez-Saez · Jan 28, 2015 · Viewed 23.9k times · Source

What's the best/cleanest way of capturing volume up/down button presses on iOS 8?

Ideally I'd like to capture the keypress and also prevent the system volume from changing (or at the very least, prevent the volume change HUD from showing).


There are some old answers going around which use deprecated methods and don't seem to work at all on iOS 8. This iOS 8 specific one didn't work either.

This RBVolumeButtons open source class doesn't seem to work on iOS 8 either.

Answer

Mohammed Elrashidy picture Mohammed Elrashidy · Apr 24, 2016

For Swift you can use next code in your viewController class:

let volumeView = MPVolumeView(frame: CGRectMake(-CGFloat.max, 0.0, 0.0, 0.0))
self.view.addSubview(volumeView)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(volumeChanged(_:)), name: "AVSystemController_SystemVolumeDidChangeNotification", object: nil)

Then add this function

func volumeChanged(notification: NSNotification) {

     if let userInfo = notification.userInfo {
        if let volumeChangeType = userInfo["AVSystemController_AudioVolumeChangeReasonNotificationParameter"] as? String {
            if volumeChangeType == "ExplicitVolumeChange" {
                // your code goes here
            }
        }
    }
}

This code detect the explicit volume change action by the user, as if you didn't check of the explicit action, this function will be automatically called periodically.

This code don't prevent the system volume change