指纹识别 touch id的简单使用

主要代码如下

#import //引入头文件


- (void)yanzheng
{
    LAContext *lac = [[LAContext alloc] init];
    BOOL isSupport = [lac canEvaluatePolicy:kLAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL];
    lac.localizedFallbackTitle = NSLocalizedString(@"请输入密码", nil);
    if (isSupport) {
        [lac evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请输入您的Touch ID" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证通过" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
                    [alert show];
                });
            }else if([error.userInfo[@"NSLocalizedDescription"] isEqualToString:@"Canceled by user."]){
                dispatch_async(dispatch_get_main_queue(), ^{
                    return ;
                });
            }else{
                dispatch_async(dispatch_get_main_queue(), ^{
                   [self alertViewWithEnterPassword:YES];
                });
            }
        }];
    }else{
        NSLog(@"当前机型不支持");
    }
}

上面这段代码是进行验证的关键代码和判断,其中包括验证成功,验证失败,和取消验证。
如果验证失败,可以弹出一个输入密码的提示框,代码如下:

- (void)alertViewWithEnterPassword:(BOOL)isTrue
{
    UIAlertController *alert;
    if (isTrue) {
        alert = [UIAlertController alertControllerWithTitle:@"请输入密码" message:@"请输入您的支付密码" preferredStyle:UIAlertControllerStyleAlert];
    }else{
        alert = [UIAlertController alertControllerWithTitle:@"密码错误" message:@"请输入您的支付密码" preferredStyle:UIAlertControllerStyleAlert];
    }
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.secureTextEntry = YES;
    }];
    UIAlertAction *backAction = [UIAlertAction actionWithTitle:@"返回指纹验证" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self yanzheng];
    }];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([alert.textFields.firstObject.text isEqualToString:@"123"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证通过" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            [self alertViewWithEnterPassword:NO];
        }
    }];
    [alert addAction:backAction];
    [alert addAction:confirmAction];
    [self presentViewController:alert animated:YES completion:nil];
}

你可能感兴趣的:(指纹识别 touch id的简单使用)