The iPhone's lock screen monitoring is divided into two ways to monitor:
1. The program is in the foreground, this is relatively simple. Just use the notifications from the Darwin layer:
#import <> #define NotificationLock CFSTR("") #define NotificationChange CFSTR("") #define NotificationPwdUI CFSTR("") 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 type is that after the program goes backstage, the notification above will not be received when the screen is locked. Another method is needed to detect whether the screen is locked in a loop, which will consume performance and may be suspended by Apple (it may not work);
static void setScreenStateCb() { uint64_t locked; __block int token = 0; notify_register_dispatch("",&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); } }
The above is the status of the iOS monitor mobile phone lock screen introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!