从通知小组件中查找设备是否已锁定

我想知道当我加载Notification / Today小部件时设备是否被锁定,所以我可以适当地显示小部件。 (这是财务方面的,我们不想在锁定的手机上显示余额)

在具有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 …但我仍然可以阅读它:(

更新:发现它。 将伊恩的答案标记为正确

在应用程序运行时使用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设备。

我在这里找到答案(间接)(最有可能需要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设备。

我在这里找到答案(间接)(最有可能需要iOS开发程序)

我试过了,我的文件总是可读的(在锁定屏幕与否)。

我找到了这个文件: https : //www.apple.com/business/docs/iOS_Security_Guide.pdf

在锁定设备10秒后,文件似乎被锁定。

知道这一点,你可以从扩展中创建文件,它似乎工作。