判断设备是否锁屏

下面代码可以判断设备是否锁屏:

在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(@"锁屏");

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容