gpt4 book ai didi

ios - 检查设备是否已锁定或解锁

转载 作者:行者123 更新时间:2023-12-03 20:25:00 25 4
gpt4 key购买 nike

我使用以下代码来检查 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。我已经启动了该应用程序,并且在任何时候我都想知道设备是否已锁定或解锁。使用上述代码,仅当用户锁定或解锁设备时我们才会收到通知。

有什么替代方案吗?

最佳答案

引用

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);
}

此外,您可以设置全局 bool 变量并设置其值,无论设备是否锁定。

关于ios - 检查设备是否已锁定或解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35932501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com