The iphone's lock screen monitoring is monitored in two ways:
1. Procedures in the foreground, this is relatively simple. Direct use of the Darwin Layer notification is available:
#import <notify.h>
#define notificationlock cfstr("Com.apple.springboard.lockcomplete")
#define notificationchange cfstr("Com.apple.springboard.lockstate")
#define Notificationpwdui cfstr("Com.apple.springboard.hasBlankedScreen")
static void screenlockstatechanged(cfnotificationcenterref Center,void* Observer,cfstringref name,const void* object, Cfdictionaryref userInfo)
{
nsstring* lockstate = (__bridge nsstring*)name;
if ([lockstate isequaltostring:(__bridge nsstring*)Notificationlock ]) {
NSLog(@"locked.") );
} else {
NSLog(@"lock state changed.") );
}
}
- (BOOL)application:(uiapplication *)Application Didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
//Override point for customization after application launch.
Cfnotificationcenteraddobserver (Cfnotificationcentergetdarwinnotifycenter (), NULL, screenlockstatechanged, Notificationlock, NULL, cfnotificationsuspensionbehaviordeliverimmediately);
Cfnotificationcenteraddobserver (Cfnotificationcentergetdarwinnotifycenter (), NULL, screenlockstatechanged, Notificationchange, NULL, cfnotificationsuspensionbehaviordeliverimmediately);
SETSCREENSTATECB ();
return YES;
}
2. The second is the program back to the stage, then lock the screen will not receive the above notification, need another way, in a circular way has been to detect whether the lock screen state, will consume performance and may be Apple hang;
static void SETSCREENSTATECB()
{
uint64_t locked;
__block int token = 0;
Notify_register_dispatch("com.apple.springboard.lockstate",&token,dispatch_get_ Main_queue(), ^ (int t) {
});
Notify_get_state(token, &locked);
NSLog(@"%d", (int)locked);
}
- (void)applicationdidenterbackground:(uiapplication *) Application
{
while (YES) {
SETSCREENSTATECB();
Sleep(1);
}
}
About Notify.h please go to:
Https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/notify_register_check.3.html
iOS app detects phone lock screen and unlocks status