从通知小部件中查明设备是否已锁定
Finding out if the device is locked, from a Notification Widget
我想知道当我加载 Notification/Today 小部件时设备是否被锁定,以便我可以适当地显示小部件。 (这是财务方面的,我们不想在锁定的 phone 上显示余额)
在带有 TouchID 的设备上,我可以尝试访问钥匙串,如果我得到
errSecInteractionNotAllowed
返回,已锁定。都好。 这不适用于没有 touchID(但有 PIN)的设备。我发现了一些东西,推荐使用
[[UIApplication sharedApplication] protectedDataAvailable]
但是我的小部件中没有 [UIApplication sharedApplication]
。
知道在哪里以及如何做到这一点吗?我只需要一个 yes/no: 设备是否锁定。
谢谢
[更新:这是我的代码]
获取文件名:
+ (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 开发程序)
经过3-4天的寻找,终于找到了答案。更多的是我如何读回结果。 Ian 是对的:我需要使用 createFileAtPath 创建文件,然后使用
读回
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 开发程序)
我试过了,我的文件总是可读的(无论是否在锁屏界面)。
我找到了这份文件:
https://www.apple.com/business/docs/iOS_Security_Guide.pdf
似乎文件在设备锁定后 10 秒被锁定。
知道了,您可以从扩展中创建文件,而且它似乎有效。
我想知道当我加载 Notification/Today 小部件时设备是否被锁定,以便我可以适当地显示小部件。 (这是财务方面的,我们不想在锁定的 phone 上显示余额)
在带有 TouchID 的设备上,我可以尝试访问钥匙串,如果我得到
errSecInteractionNotAllowed
返回,已锁定。都好。 这不适用于没有 touchID(但有 PIN)的设备。我发现了一些东西,推荐使用
[[UIApplication sharedApplication] protectedDataAvailable]
但是我的小部件中没有 [UIApplication sharedApplication]
。
知道在哪里以及如何做到这一点吗?我只需要一个 yes/no: 设备是否锁定。
谢谢
[更新:这是我的代码]
获取文件名:
+ (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 开发程序)
经过3-4天的寻找,终于找到了答案。更多的是我如何读回结果。 Ian 是对的:我需要使用 createFileAtPath 创建文件,然后使用
读回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 开发程序)
我试过了,我的文件总是可读的(无论是否在锁屏界面)。
我找到了这份文件: https://www.apple.com/business/docs/iOS_Security_Guide.pdf
似乎文件在设备锁定后 10 秒被锁定。
知道了,您可以从扩展中创建文件,而且它似乎有效。