I want my application monitor to be locked and unlocked, and also when it becomes empty (after longer inactivity), all this while my application is not focused, but works in the background.
I easily get lock / unlock / empty events when the application is focused:
-(void) startListeningForPhoneLockEvent { NSLog(@"Start listening to lock/unlock and screen-goes-black events."); CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (void*)self, lockStateChanged, CFSTR("com.apple.springboard.lockstate"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (void*)self, hasBlankedScreen, CFSTR("com.apple.springboard.hasBlankedScreen"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); }
And callback functions:
static void lockStateChanged( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo ) { NSLog(@"Lock event received!"); } static void hasBlankedScreen( CFNotificationCenterRef center, void*observer, CFStringRef name, const void *object, CFDictionaryRef userInfo ) { NSLog(@"Blanked screen event received!"); }
I turned on the background mode:
However, when the application goes into the background, it does not accept lock / unlock / blank screen events.
I tried using other background modes such as playing sound, location updates, etc., but the application still does not accept lock / unlock / blank screen events in the background.
I am not sure if this is really possible, or if I am doing something wrong.
I am testing it on a real device that is upgrading to iOS9 using the latest Xcode with the iOS9 SDK.