我想知道当我加载我的通知/今天小部件时设备是否被锁定,所以我可以适当地显示小部件。(这是财务问题,我们不想在锁定的手机上显示余额)
在带有 TouchID 的设备上,我可以尝试访问钥匙串,如果我得到
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 的答案标记为正确