iOS 判断解屏和锁屏状态

在AppDelegate中实现如下方法

//锁屏
#import 
#define NotificationLock CFSTR("com.apple.springboard.lockcomplete")
#define NotificationChange CFSTR("com.apple.springboard.lockstate")
#define NotificationPwdUI CFSTR("com.apple.springboard.hasBlankedScreen")

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

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

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, screenLockStateChanged, NotificationChange, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
return YES;
}
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(@"状态改变了");

    }
}

这里要更正一下,这个方法在苹果新出的审核规则里,已经被拒绝了,所以开发者们可以通过appleDelegate中的applicationWillResignActive方法配合NSNotificationCenter来检测锁屏问题以及HOME键事件。

你可能感兴趣的:(iOS 判断解屏和锁屏状态)