我想使用TouchID验证我自己的应用。
1.我希望用户可以点击“输入密码”来调用系统内置密码屏幕进行身份验证,如果成功则输入我自己的应用程序。
但我不知道如何将它转发到密码验证视图,如下面的屏幕'案例LAErrorUserFallback'
这是我的代码:
LAContext *context = [[LAContext alloc] init];
__block NSString *msg;
__block BOOL bAuth;
// show the authentication UI with our reason string
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"Unlock APP With FingerPrint", nil) reply:
^(BOOL success, NSError *authenticationError) {
if (success) {
bAuth = YES;
msg =[NSString stringWithFormat:NSLocalizedString(@"EVALUATE_POLICY_SUCCESS", nil)];
dispatch_async(dispatch_get_main_queue(), ^{
[[MYAppDelegate theDelegate] initializeAppAfterKeyVaultUnlocked];
});
NSLog(@"%@",msg);
} else {
bAuth = NO;
switch (authenticationError.code) {
case LAErrorAuthenticationFailed:
msg = [NSString stringWithFormat:NSLocalizedString(@"Authentication Failed", nil)];
// ...
break;
case LAErrorUserCancel:
msg = [NSString stringWithFormat:NSLocalizedString(@"User pressed Cancel button", nil)];
dispatch_async(dispatch_get_main_queue(), ^{
[[MYAppDelegate theDelegate] exitAndLock];
});
break;
case LAErrorUserFallback:
msg = [NSString stringWithFormat:NSLocalizedString(@"User pressed \"Enter Password\"", nil)];
//Here I want to fallback to iOS build-in passcode authenticate view, and get the auth result.
break;
default:
msg = [NSString stringWithFormat:NSLocalizedString(@"Touch ID is not configured", nil)];
// ...
break;
}
NSLog(@"%@",authenticationError.localizedDescription);
}
}];
答案 0 :(得分:13)
现在在iOS 9中实际上非常简单 - 您只需使用LAPolicyDeviceOwnerAuthentication
代替LAPolicyDeviceOwnerAuthenticationWithBiometrics
所以在你的代码中你只需要替换它:
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"Unlock APP With FingerPrint", nil) reply:
^(BOOL success, NSError *authenticationError) {
有了这个:
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:NSLocalizedString(@"Unlock APP With FingerPrint", nil) reply:
^(BOOL success, NSError *authenticationError) {
因此,当用户无法通过指纹进行身份验证时,必须输入密码"选项将调用系统密码输入屏幕。
答案 1 :(得分:1)
根据我的理解,如果要使用evaluatePolicy,则必须自己构建密码屏幕。
如果使用钥匙串,如果指法失败,SecItemCopyMatching功能会自动回退到密码。以下是如何推动它的参考(参见第3节):https://www.secsign.com/fingerprint-validation-as-an-alternative-to-passcodes/
答案 2 :(得分:1)
没有尝试过,但是这篇帖子声称您可以按照以下here使用系统(这仅适用于iOS 8或更高版本)。
或者(这就是我所做的)你可以构建你的密码输入屏幕(以支持旧的iOS版本),我的控制器有一个密码输入视图,当用户选择使用密码时它将被暴露。此时,来自evaluatePolicy的回调将返回LAErrorUserFallback,这可能是打开自定义密码屏幕的时间。
类似的东西:
-(void)maybeShowTouchIDMessage {
if (![SettingsManager sharedManager].isUseTouchID || self.createPassCodeMode) {
self.shieldView.hidden = YES;
return;
}
self.shieldView.hidden = NO;
self.shieldView.alpha = 1.0;
LAContext *context = [[LAContext alloc] init];
NSError *evalError = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&evalError] ) {
__weak SecurityWindowViewController *weakSelf = self;
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"Use touch id \n or hit \"Cancel\" to enter passcode"
reply:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
SecurityWindowViewController *strongSelf = weakSelf;
if (success) {
[strongSelf hideWithSuccess:YES];
} else if (error){
NSString *errorMessage;
BOOL showError = NO;
switch (error.code) {
case LAErrorAuthenticationFailed:
errorMessage = @"Sorry couldn't autheticate";
showError = YES;
break;
case LAErrorPasscodeNotSet:
errorMessage = @"Sorry couldn't autheticate";
showError = YES;
break;
case LAErrorTouchIDNotEnrolled:
errorMessage = @"Touch ID has no enrolled fingers";
showError = YES;
break;
default:
showError = NO;
break;
}
[UIView animateWithDuration:0.5 animations:^{
strongSelf.shieldView.alpha = 0.0;
} completion:^(BOOL finished) {
strongSelf.shieldView.hidden = YES;
}];
if (showError) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:errorMessage
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
}
});
}];
}
答案 3 :(得分:1)
针对您的情况:
case LAErrorUserFallback:
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:NSLocalizedString(@"Unlock APP With PassCode", nil) reply: ^(BOOL success, NSError *authenticationError) {
if(success){
NSLog(@"PassCode Login successful");
}else{
NSLog(@"%@",authenticationError);
}
}
对于设备密码验证,您需要使用LAPolicyDeviceOwnerAuthentication而不是LAPolicyDeviceOwnerAuthenticationWithBiometrics。希望这对您有帮助!
答案 4 :(得分:0)
您可以添加另一个案例并从中调用您的密码屏幕。
这是我的代码:
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = strMessage;
objFlockr.pVerificationBlock = objResponse;
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
if (!isShow) {
myContext.localizedFallbackTitle = @"";
}
else
{
myContext.localizedFallbackTitle = @"Set Up Passcode";
}
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL succes, NSError *error) {
if (!AppDel.firstAttampt && succes && !isShow)
{
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(1);
}
else if (succes) {
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(0);
NSLog(@"User authenticated");
} else {
switch (error.code) {
case LAErrorAuthenticationFailed:
NSLog(@"Authentication Failed");
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(1);
break;
case LAErrorUserCancel:
NSLog(@"User pressed Cancel button");
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(3);
break;
case LAErrorUserFallback:
NSLog(@"User pressed \"Enter Password\"");
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(4);
break;
default:
[self showMessage:@"Touch ID is not configured" withTitle:@"Error"];
if (objFlockr.pVerificationBlock)
objFlockr.pVerificationBlock(2);
NSLog(@"Touch ID is not configured");
break;
}
NSLog(@"Authentication Fails");
}
}];
} else {
NSLog(@"Can not evaluate Touch ID");
[self showMessage:@"Can not evaluate TouchID" withTitle:@"Error"];
}
答案 5 :(得分:0)
将LAPolicy
策略枚举值deviceOwnerAuthenticationWithBiometrics
替换为deviceOwnerAuthentication
注意:如果用户启用了
biometric
(面部ID或触摸ID)身份验证,则设备将首先要求进行生物特征认证,如果用户选择了后备身份验证,则仅{{ 1}}将显示密码屏幕。
尝试一下,看看(速记4):
deviceOwnerAuthentication