指紋識別 touch id的簡單使用

主要代碼如下

#import <LocalAuthentication/LocalAuthentication.h>//引入頭文件


- (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(@"當前機型不支持");
    }
}

上面這段代碼是進行驗證的關(guān)鍵代碼和判斷,其中包括驗證成功,驗證失敗,和取消驗證。
如果驗證失敗,可以彈出一個輸入密碼的提示框,代碼如下:

- (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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,436評論 25 708
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,536評論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,246評論 4 61
  • 河套的故事 文字的視頻 ——讀劉文忠長篇小說《魔咒》 越玉柱 與其說《...
    塞上春陽閱讀 254評論 0 1
  • 愛情應(yīng)該是什么樣子? 在這熙熙攘攘上的世界,談愛情,似乎是一件非常奢侈的事情。 前段時間看了一篇文章,《90后婚姻...
    小小言午閱讀 754評論 4 11