【问题标题】:Check device is locked or unlocked检查设备是否被锁定或解锁
【发布时间】:2016-03-11 04:53:28
【问题描述】:

我使用以下代码来检查 iphone 锁定状态。

int notify_token;

notify_register_dispatch("com.apple.springboard.lockstate", &notify_token,dispatch_get_main_queue(), ^(int token) {

    uint64_t state = UINT64_MAX;
    notify_get_state(token, &state);

    if (state == 0)
    {
        // Locked
    }
    else
    {
        // Unlocked
    }
});

问题是我们只有在设备被锁定或解锁时才会收到通知。我想知道当前的锁定状态。 IE。我已经启动了该应用程序,并且在任何时候我都想知道设备是锁定还是解锁。使用上面的代码,我们只会在设备被用户锁定或解锁时收到通知。

有没有其他选择?

【问题讨论】:

标签: ios objective-c iphone locked


【解决方案1】:

参考

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    // the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
    CFStringRef nameCFString = (CFStringRef)name;
    NSString *lockState = (NSString*)nameCFString;
    NSLog(@"Darwin notification NAME = %@",name);

    if([lockState isEqualToString:@"com.apple.springboard.lockcomplete"])
    {
        NSLog(@"DEVICE LOCKED");
        //Logic to disable the GPS
    }
    else
    {
        NSLog(@"LOCK STATUS CHANGED");
        //Logic to enable the GPS
    }
}

-(void)registerforDeviceLockNotif
{
    //Screen lock notifications
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                NULL, // observer
                                displayStatusChanged, // callback
                                CFSTR("com.apple.springboard.lockcomplete"), // event name
                                NULL, // object
                                  CFNotificationSuspensionBehaviorDeliverImmediately);

     CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                NULL, // observer
                                displayStatusChanged, // callback
                                CFSTR("com.apple.springboard.lockstate"), // event name
                                NULL, // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);
}

此外,您可以设置全局布尔变量并设置其值是否设备被锁定。

【讨论】:

  • 在 iOS 10 上,我先得到 lockcomplete,然后得到 lockstate。如何使用此代码来跟踪手机是锁定还是解锁? iOS 10 是否改变了这一点?
  • 这个用法现在被拒绝了。
  • 在哪里调用这个方法,应该传递什么参数?
  • @RhythmicFistman 是他们在 ios 中实现锁定解锁状态的任何其他方式,苹果允许我们发布这些应用程序。
【解决方案2】:

斯威夫特:

/**
 Start listen "Device Locked" notifications
 */
func registerforDeviceLockNotif() {
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                nil,
                                {(center: CFNotificationCenter!, observer: UnsafeMutablePointer<Void>, name: CFString!, object: UnsafePointer<Void>, userInfo: CFDictionary!) -> Void in
                                    // the "com.apple.springboard.lockcomplete" notification will always come after the "com.apple.springboard.lockstate" notification
                                    let lockState = name as String
                                    if lockState == "com.apple.springboard.lockcomplete" {
                                        NSLog("DEVICE LOCKED");
                                    }
                                    else {
                                        NSLog("LOCK STATUS CHANGED");
                                    }

        },
                                "com.apple.springboard.lockcomplete",
                                nil,
                                CFNotificationSuspensionBehavior.DeliverImmediately)
}

【讨论】:

    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多