从通知小部件中查找设备是否被锁定 [英] Finding out if the device is locked, from a Notification Widget
问题描述
我想知道在加载通知/今天"窗口小部件时设备是否已锁定,因此我可以适当地显示该窗口小部件. (这是财务问题,我们不想在锁定的电话上显示余额)
I'd like to know if the device is locked when I'm loading my Notification/Today widget, so I can show the widget appropriately. (it's financial, and we don't want to show balances on a locked phone)
在具有TouchID的设备上,我可以尝试访问钥匙串,如果可以得到
On devices with TouchID, I can just try to access the Keychain, and if I get
errSecInteractionNotAllowed
返回,它已被锁定.都好. 这在没有touchID(但带有PIN码)的设备上不起作用.我发现了一些建议使用的建议
back, it's locked. All good. This doesn't work on devices without touchID (but with a PIN). I've found a few things, which recommend using
[[UIApplication sharedApplication] protectedDataAvailable]
但是我在小部件中没有[UIApplication sharedApplication]
.
However I don't have [UIApplication sharedApplication]
in a widget.
任何想法在哪里以及如何做到这一点?我只需要是/否:设备是否已锁定.
Any ideas where and how to do this? I just need a yes/no: is the device locked.
谢谢
[更新:这是我的代码]
[UPDATE: here's the code I have]
获取文件名:
+ (NSString *)lockedDeviceFilename {
NSURL *directoryUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:USER_DEFAULTS_GROUP_NAME];
return [directoryUrl.path stringByAppendingPathComponent:@"security.dummy"];
}
编写/创建文件(在应用程序中,而不是扩展名中:
Writing / creating the file (in the app, not the extension:
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 ...,但我仍然可以阅读它:(
It's always able to read the file, even on a TouchID device with the screen locked. If I look at the attributes, it shows the NSFileProtectionKey is set to NSFileProtectionComplete... but I can STILL READ IT :(
更新:找到它.将伊恩的答案标记为正确
Update: found it. Marking Ian's answer as correct
推荐答案
在应用运行时,使用NSFileProtectionComplete
创建文件,然后尝试从扩展程序访问该文件.如果您无法访问它,则屏幕将被锁定.
Create a file with NSFileProtectionComplete
while your app is running and then attempt to access it from your extension. If you can't access it, the screen is locked.
[[NSFileManager defaultManager] createFileAtPath:someFilePath
contents:[@"Lock screen test." dataUsingEncoding:NSUTF8StringEncoding]
attributes:@{NSFileProtectionKey: NSFileProtectionComplete}];
编辑:包括完成解决方案和合并答案的最终步骤. (剩余的工作由Nic Wise提供.)
EDIT: Final steps included to complete solution and consolidate answers. (Remaining work provided by 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设备.
The other method, using errSecInteractionNotAllowed
also works, but only for TouchID devices.
我(间接地)找到了答案此处(大多数iOS开发者程序都使用此方法可能需要)
I found the answer (indirectly) here (rego with the iOS dev program most likely needed)
这篇关于从通知小部件中查找设备是否被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!