【问题标题】:Finding out if the device is locked, from a Notification Widget从通知小部件查明设备是否已锁定
【发布时间】:2015-01-13 23:52:51
【问题描述】:

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

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

【问题讨论】:

  • 检查文件是否存在与验证文件是否可读不同。

标签: ios objective-c ios8-today-widget today-extension


【解决方案1】:

在您的应用程序运行时使用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 开发程序的 rego)

【讨论】:

  • 谢谢伊恩 - 我试过了,但我是从扩展中创建的。我会从应用程序再试一次。谢谢
  • 将 Ian 的答案标记为正确,因为他大部分时间都在那里 - 请参阅我自己的回复以了解如何读回它(你需要两者,obv)
  • 我更新了答案以包含您的工作,以便将所有内容集中在一个地方。
  • 这一切都很好,但只有在用户有密码时才有效
【解决方案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 设备。

我找到了答案(间接地)here(很可能需要使用 iOS 开发程序进行 rego)

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多