下面截取关键代码的图片
1 #import <LocalAuthentication/LocalAuthentication.h>//引入头文件 2 3 4 - (void)yanzheng 5 { 6 LAContext *lac = [[LAContext alloc] init]; 7 BOOL isSupport = [lac canEvaluatePolicy:kLAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]; 8 lac.localizedFallbackTitle = NSLocalizedString(@"请输入密码", nil); 9 if (isSupport) { 10 [lac evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请输入您的Touch ID" reply:^(BOOL success, NSError * _Nullable error) { 11 if (success) { 12 dispatch_async(dispatch_get_main_queue(), ^{ 13 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证通过" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil]; 14 [alert show]; 15 }); 16 }else if([error.userInfo[@"NSLocalizedDescription"] isEqualToString:@"Canceled by user."]){ 17 dispatch_async(dispatch_get_main_queue(), ^{ 18 return ; 19 }); 20 }else{ 21 dispatch_async(dispatch_get_main_queue(), ^{ 22 [self alertViewWithEnterPassword:YES]; 23 }); 24 } 25 }]; 26 }else{ 27 NSLog(@"当前机型不支持"); 28 } 29 }
上面这段代码是进行验证的关键代码和判断,其中包括验证成功,验证失败,和取消验证。
如果验证失败,可以弹出一个输入密码的提示框,代码如下
1 - (void)alertViewWithEnterPassword:(BOOL)isTrue 2 { 3 UIAlertController *alert; 4 if (isTrue) { 5 alert = [UIAlertController alertControllerWithTitle:@"请输入密码" message:@"请输入您的支付密码" preferredStyle:UIAlertControllerStyleAlert]; 6 }else{ 7 alert = [UIAlertController alertControllerWithTitle:@"密码错误" message:@"请输入您的支付密码" preferredStyle:UIAlertControllerStyleAlert]; 8 } 9 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 10 textField.secureTextEntry = YES; 11 }]; 12 UIAlertAction *backAction = [UIAlertAction actionWithTitle:@"返回指纹验证" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 13 [self yanzheng]; 14 }]; 15 16 UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 17 if ([alert.textFields.firstObject.text isEqualToString:@"123"]) { 18 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证通过" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil]; 19 [alert show]; 20 }else{ 21 [self alertViewWithEnterPassword:NO]; 22 } 23 }]; 24 [alert addAction:backAction]; 25 [alert addAction:confirmAction]; 26 [self presentViewController:alert animated:YES completion:nil]; 27 }