Ios 我可以检查用户是否设置了密码吗?
我们有一个存储敏感数据的应用程序。我们已经启用了文件保护,但这只有在用户设置了密码时才有效。如果用户没有设置密码,我们需要显示一个警报,告诉用户这样做,然后不要加载应用程序的其余部分 基本上,我们的处境和中一样,我的问题就是他们的问题。但公认的答案是“启用文件保护”,这不是对这个问题的回答,也不是对这个问题的回答;我已经启用了文件保护,但它并没有告诉我他们是否设置了密码Ios 我可以检查用户是否设置了密码吗?,ios,security,Ios,Security,我们有一个存储敏感数据的应用程序。我们已经启用了文件保护,但这只有在用户设置了密码时才有效。如果用户没有设置密码,我们需要显示一个警报,告诉用户这样做,然后不要加载应用程序的其余部分 基本上,我们的处境和中一样,我的问题就是他们的问题。但公认的答案是“启用文件保护”,这不是对这个问题的回答,也不是对这个问题的回答;我已经启用了文件保护,但它并没有告诉我他们是否设置了密码 那么,是否有可能检查,如果有,如何检查?理想情况下,我们希望检查用户是否设置了长密码或简单密码,如果他们只设置了简单密码,我们
那么,是否有可能检查,如果有,如何检查?理想情况下,我们希望检查用户是否设置了长密码或简单密码,如果他们只设置了简单密码,我们会警告他们设置正确的密码。对于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
}
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可用。它可以用来获得一些结果,但这些结果并不能回答您的问题。好答案,适用于iOS10。在Swift3中也是一样:
func设备hasPassCodeEnabled()->Bool{let myContext=lacContext()var authError:NSError?=nil如果(myContext.CaneValuePolicy(.deviceOwnerAuthentication,error:&authError)){return true}否则{return false}
它在iOS 11(测试版)上运行吗?我们面临iOS 11(测试版)的问题