美文网首页
判断设备是否锁屏

判断设备是否锁屏

作者: MaxMak | 来源:发表于2016-06-21 18:34 被阅读0次

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

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

    }

    }

    相关文章

      网友评论

          本文标题:判断设备是否锁屏

          本文链接:https://www.haomeiwen.com/subject/gmugdttx.html