5

Possible Duplicate:
program access to iPhone volume buttons

Is it possible to detect a volume up button press/release in an iOS app?

Community
  • 1
  • 1
thisiscrazy4
  • 1,905
  • 8
  • 35
  • 58

2 Answers2

5

You can detect the volume button press in your application, but it uses a private API of Apple. Since they don't allow you to use their private APIs in your application, this leads straight away to the rejection your application, so use this at your own risk.

In your viewDidLoad:, for example:

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(18, 340, 284, 23)];
[self.view addSubview:volumeView];

[NSNotificationCenter.defaultCenter
    addObserver:self
       selector:@selector(volumeDidChange:)
           name:@"AVSystemController_SystemVolumeDidChangeNotification"
         object:nil];
freya
  • 459
  • 7
  • 14
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79
  • Disregard previous comment...it does work in iOS10. I simply hadn't added the volume view, it doesn't work without that (notifications don't get generated). In any case, do note that you can make the frame CGRectZero, which is probably safer since it means there's no possible way it will ever appear or receive touch events. You can also add it to the UIApplication singleton's keyWindow if you don't want to do this from within a view controller. :) – Reid Nov 14 '16 at 18:59
3

Take a look at http://fredandrandall.com/blog/2011/11/18/taking-control-of-the-volume-buttons-on-ios-like-camera/

Essentially you have to initialize an audio session, make it active, and then listen for changes. Finally you initiate a callback.

However, be careful with your implementations of hardware commands. Unintentional misuse can get you banned from the app store.

The Obscure Question
  • 1,134
  • 11
  • 26
  • What do you mean by "unintentional misuse"? What if you want to trigger a legit "no look" action similar to how the camera app works? – JackAce Apr 04 '14 at 22:13