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


i have app trigger when user presses power button several times. app works background process , listens cfnotificationcenterref triggered when display status changes. sensitive. when device receives kind of notification display automatically changes triggers event. there way detect power button presses?

much of code borrowed detect screen on/off ios service

i register observer background process

    cfnotificationcenteraddobserver(cfnotificationcentergetdarwinnotifycenter(), //center                                     null, // observer                                     displaystatuschanged, // callback                                     cfstr("com.apple.iokit.hid.displaystatus"), // event name                                     null, // object                                     cfnotificationsuspensionbehaviordeliverimmediately); 

this code handles 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];      // might try inspecting `userinfo` dictionary, see     //  if contains useful info     if (userinfo != nil) {         cfshow(userinfo);     }  } 

here 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 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 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 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 6 seconds ago 


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -