5

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

在带有 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 的答案标记为正确

4

3 回答 3

8

在您的应用程序运行时创建一个文件,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 设备。

我在这里找到了答案(间接)(rego with the iOS dev program most likely need)

于 2015-01-14T00:54:14.217 回答
2

终于,经过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 设备。

我在这里找到了答案(间接)(rego with the iOS dev program most likely need)

于 2015-01-14T02:49:11.193 回答
1

我试过了,我的文件总是可读的(无论是否在锁屏中)。

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

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

知道了这一点,您可以从扩展名创建文件,并且它似乎可以工作。

于 2015-04-24T12:51:13.380 回答