gpt4 book ai didi

ios - 从通知小部件中查明设备是否已锁定

转载 作者:可可西里 更新时间:2023-11-01 04:20:41 24 4
gpt4 key购买 nike

我想知道在我加载通知/今天小部件时设备是否已锁定,以便我可以适本地显示小部件。 (这是财务方面的,我们不想在锁定的手机上显示余额)

在带有 TouchID 的设备上,我可以尝试访问钥匙串(keychain),如果我得到

errSecInteractionNotAllowed

返回,它被锁定了。都好。 这不适用于没有 touchID 的设备(但有 PIN)。我发现了一些东西,推荐使用

[[UIApplication sharedApplication] protectedDataAvailable]

但是我的小部件中没有 [UIApplication sharedApplication]

知道在哪里以及如何做到这一点吗?我只需要一个是/否:设备是否已锁定。

谢谢

[更新:这是我的代码]

获取文件名:

+ (NSString *)lockedDeviceFilename {
NSURL *directoryUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:USER_DEFAULTS_GROUP_NAME];
return [directoryUrl.path stringByAppendingPathComponent:@"security.dummy"];
}

编写/创建文件(在应用程序中,而不是扩展名中:

NSError *error = nil;

NSString *documentPath = [FOOStorageGatekeeper lockedDeviceFilename];

[[NSFileManager defaultManager] removeItemAtPath:documentPath error:&error];

BOOL created = [[NSFileManager defaultManager] createFileAtPath:documentPath
contents:[@"super secret file contents. we only care about the permissions" dataUsingEncoding:NSUTF8StringEncoding]
attributes:@{NSFileProtectionKey : NSFileProtectionComplete}];

阅读:

 BOOL isReadable = [[NSFileManager defaultManager] fileExistsAtPath:[FOOStorageGatekeeper lockedDeviceFilename]];

NSLog(@"isReadable? %@", isReadable ? @"YES" : @"NO");

它始终能够读取文件,即使在屏幕锁定的 TouchID 设备上也是如此。如果我查看属性,它会显示 NSFileProtectionKey 已设置为 NSFileProtectionComplete...但我仍然可以阅读它:(

更新:找到了。将 Ian 的答案标记为正确

最佳答案

在您的应用程序运行时使用 NSFileProtectionComplete 创建一个文件,然后尝试从您的扩展程序访问它。如果您无法访问它,则屏幕已锁定。

[[NSFileManager defaultManager] createFileAtPath:someFilePath
contents:[@"Lock screen test." dataUsingEncoding:NSUTF8StringEncoding]
attributes:@{NSFileProtectionKey: NSFileProtectionComplete}];

编辑:包括完成解决方案和合并答案的最后步骤。 (剩余工作由 Nic Wise 提供。)

NSData *data = [NSData dataWithContentsOfURL:[FOOStorageGatekeeper lockedDeviceUrl] options: NSDataReadingMappedIfSafe error:&error];

if (error != nil && error.code == 257) {
NSLog(@"**** the keychain appears to be locked, using the file method");
return YES;
}

另一种方法,使用 errSecInteractionNotAllowed 也有效,但仅适用于 TouchID 设备。

我找到了答案(间接)here (重新使用最有可能需要的 iOS 开发程序)

关于ios - 从通知小部件中查明设备是否已锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933666/

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