3

I am creating an app, which needs to do something when the user presses the power button 5 times.

I figured out that it's difficult to implement in iOS, but I think it's not impossible. How do I listen for power key events, even when the app is running in the background?

Can anyone help me to find solution?

jscs
  • 63,694
  • 13
  • 151
  • 195
satvinder singh
  • 1,152
  • 1
  • 12
  • 22
  • 2
    There is no way to tell the "power" key has been pressed. The closest you might get is that the app will resign active, but there's no indication as to why that has happened. And of course, that will only happen once, not 5 times. – Avi Dec 01 '15 at 06:23

3 Answers3

1

you can tap a power key once and also you cannot detect the event from your application, this is not possible in iOS as far now, better try a different way to send alert with in your app, set some conditions with in your app and then send a alert based on those conditions if satisfied, I think this would be better,

Somehow ,If you try to override the existing functionality of the power key, apple will reject your app I think so,

satheesh
  • 2,770
  • 2
  • 14
  • 19
1

You can't directly get the power button events. But there are notifications which you can count like UIApplicationProtectedDataWillBecomeUnavailable or UIApplicationWillResignActiveNotification. Or just register for all low level notifications with CFNotificationCenter and see if you find something fitting like com.apple.springboard.lockstate.

orkoden
  • 18,946
  • 4
  • 59
  • 50
0

I don't think you can override system level actions like holding the power button, pressing the home button, overriding the mute sound switch within your own app. iOS system doesn't exactly behave like an normal computer OS, it's to much more limited.

Apple is not allowing you to use hardware components completely. They have added some restrictions. They provided the method in the app delegate i.e. applicationDidEnterBackground can catch the home button press .Also they has provided the the API's to access the camera,bluetooth etc .At least this much of API's I know which provided by apple publicly to access the hardware. You cannot access the other hardware elements in your application which not provided publicly by apple .If you are able to do this by any way then also your application will not approved by apple .

Vvk
  • 4,031
  • 29
  • 51