我们有一个存储敏感数据的应用。我们已启用文件保护,但只有在用户设置了密码时才会生效。如果用户没有设置密码,我们需要显示警告,告诉用户这样做,然后不要加载应用程序的其余部分。
基本上我们处于this question的确切情况,我的问题正是他们的问题。但是接受的答案是"启用文件保护",这不是这个问题的答案,或者是这个问题;我已经启用了文件保护功能,但它并没有告诉我他们是否设置了密码。
那么可以检查,如果可以,如何检查?理想情况下,我们要检查用户是设置了长密码还是简单密码,如果他们只设置了一个简单的密码,我们会警告他们设置一个合适的密码。
答案 0 :(得分:7)
iOS 9对此问题有正式答案:
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&authError])
{
// Device has either passcode enable (or passcode and touchID)
}
else
{
// Device does not have a passcode
// authError can be checked for more infos (is of type LAError)
}
的链接
注意: LAPolicyDeviceOwnerAuthentication 是仅在iOS 9中可用的常量。 iOS 8提供了 LAPolicyDeviceOwnerAuthenticationWithBiometrics 。它可用于实现一些结果,但这些不能回答你的问题。
答案 1 :(得分:3)
使用iOS 8,现在有一种方法可以检查用户是否设置了密码。 此代码将在iOS 7上崩溃。
目标-C:
-(BOOL) deviceHasPasscode {
NSData* secret = [@"Device has passcode set?" dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *attributes = @{ (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, (__bridge id)kSecAttrService: @"LocalDeviceServices", (__bridge id)kSecAttrAccount: @"NoAccount", (__bridge id)kSecValueData: secret, (__bridge id)kSecAttrAccessible: (__bridge id)kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly };
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
if (status == errSecSuccess) { // item added okay, passcode has been set
SecItemDelete((__bridge CFDictionaryRef)attributes);
return true;
}
return false;
}
夫特:
func deviceHasPasscode() -> Bool {
let secret = "Device has passcode set?".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let attributes = [kSecClass as String:kSecClassGenericPassword, kSecAttrService as String:"LocalDeviceServices", kSecAttrAccount as String:"NoAccount", kSecValueData as String:secret!, kSecAttrAccessible as String:kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly]
let status = SecItemAdd(attributes, nil)
if status == 0 {
SecItemDelete(attributes)
return true
}
return false
}