判斷設備是否鎖屏

下面代碼可以判斷設備是否鎖屏:

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

}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容