如何检测 iOS 设备解锁/锁屏?
2017-06-17 本文已影响622人
张嘉夫
//回调
static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
// "com.apple.springboard.lockcomplete" 通知总是接在 "com.apple.springboard.lockstate" 通知后面
CFStringRef nameCFString = (CFStringRef)name;
NSString *lockState = (NSString*)nameCFString;
NSLog(@"Darwin notification NAME = %@",name);
if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"])
{
NSLog(@"锁屏");
//应用逻辑
}
else
{
NSLog(@"锁屏状态改变");
//应用逻辑
}
}
-(void)registerforDeviceLockNotif
{
//锁屏通知
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockcomplete"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
NULL, // observer
displayStatusChanged, // callback
CFSTR("com.apple.springboard.lockstate"), // event name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately);
}
注意:“com.apple.springboard.lockcomplete” 通知总是会接在 “com.apple.springboard.lockstate” 后面