下面代码可以判断设备是否锁屏:
在AppDelegate中添加头文件
#include<notify.h>
在application:didFinishLaunchingWithOptions:中添加以下代码:
```
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, handleLockStateNotification, CFSTR("com.apple.springboard.lockstate"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, handleDisplayStatusNotification, CFSTR("com.apple.iokit.hid.displayStatus"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
```
注:加粗部分为方法名
handleLockStateNotification:
static void handleLockStateNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
uint64_t state;
int token;
notify_register_check("com.apple.springboard.lockstate", &token);
notify_get_state(token, &state);
notify_cancel(token);
if ((uint64_t)1 == state)
{
// NSLog(@"锁屏");
}
else
{
// NSLog(@"解锁");
}
}
handleDisplayStatusNotification:
static void handleDisplayStatusNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
if (userInfo)
{
CFShow(userInfo);
}
uint64_t state;
int token;
notify_register_check("com.apple.iokit.hid.displayStatus", &token);
notify_get_state(token, &state);
notify_cancel(token);
if ((uint64_t)1 == state)
{
NSLog(@"解锁");
}
else
{
NSLog(@"锁屏");
}
}