How can I trigger an event only when the power button is pressed in iOS?

818 views Asked by At

I have an app that I would like to trigger when the user presses the power button several times. The app works currently as a background process and listens for CFNotificationCenterRef which is triggered when the display status changes. This is too sensitive. When the device receives any kind of notification the display automatically changes which also triggers the event. Is there a way to only detect power button presses?

Much of the code is borrowed from Detect screen on/off from iOS service

I register the observer as a background process

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    displayStatusChanged, // callback
                                    CFSTR("com.apple.iokit.hid.displayStatus"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);

This is code handles the event.

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {

    NSLog(@"event received!");
    NSLog(@"Darwin notification center = %@",center);
    NSLog(@"Darwin notification observer = %@",observer);
    NSLog(@"Darwin notification NAME = %@",name);
    NSLog(@"Darwin notification object = %@",object);
    NSLog(@"Darwin notification userInfo = %@",userInfo);

    [masterViewController powerButtonTrigger];

    // you might try inspecting the `userInfo` dictionary, to see
    //  if it contains any useful info
    if (userInfo != nil) {
        CFShow(userInfo);
    }

}

Here is a log of these events:

2015-06-14 09:17:56.715 MayDay[1268:273855] event received!
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:56.716 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:56.717 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:56.717 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:56.717 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:56.717 MayDay[1268:273855] press number 1 first press was 3 seconds ago
2015-06-14 09:17:57.452 MayDay[1268:273855] event received!
2015-06-14 09:17:57.453 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:57.454 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:57.566 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:57.566 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:57.582 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:57.583 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:57.583 MayDay[1268:273855] press number 2 first press was 4 seconds ago
2015-06-14 09:17:58.896 MayDay[1268:273855] event received!
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:58.898 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:58.899 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:58.899 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:58.900 MayDay[1268:273855] press number 3 first press was 5 seconds ago
2015-06-14 09:17:59.653 MayDay[1268:273855] event received!
2015-06-14 09:17:59.654 MayDay[1268:273855] Darwin notification center = <CFNotificationCenter 0x1740f5e00 [0x195da9f50]>
2015-06-14 09:17:59.655 MayDay[1268:273855] Darwin notification observer = (null)
2015-06-14 09:17:59.770 MayDay[1268:273855] Darwin notification NAME = com.apple.iokit.hid.displayStatus
2015-06-14 09:17:59.770 MayDay[1268:273855] Darwin notification object = (null)
2015-06-14 09:17:59.783 MayDay[1268:273855] Darwin notification userInfo = (null)
2015-06-14 09:17:59.784 MayDay[1268:273855] powerButtonTrigger
2015-06-14 09:17:59.784 MayDay[1268:273855] press number 4 first press was 6 seconds ago
0

There are 0 answers