下面代碼可以判斷設(shè)備是否鎖屏:
在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(@"鎖屏");
}
}