iOS开发之检测设备锁屏

Bison

"身体是革命的本钱,多锻炼才是王道" Bison

1
冰之依韩版女装(程序媛福利)

最近的项目里面有镶嵌的游戏,一进入锁屏app就会崩溃。 寻求解决之策,找了很久。

代码如下

更多经验请点击

核心代码

#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")

#define NotificationChange CFSTR("com.apple.springboard.lockstate")

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    
    [self lookLockScreen];

    return YES;
    
}

- (void)lookLockScreen
{
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationLock, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);


}

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.");

    }

}


博主app上线了,赶紧点此来围观吧😄


分享文章