iOS 獲取Home鍵指紋驗證


類似於支付寶的指紋支付一樣,在項目中添加指紋驗證

首先應該引入系統庫: LocalAuthentication.framework

在調用的頁面引入頭信息:#import "LocalAuthentication/LAContext.h"

    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"我們需要驗證您的指紋來確認你的身份";
    // 判斷設備是否支持指紋識別
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
        // 指紋識別只判斷當前用戶是否機主
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
                                if (success) {
                                    // User authenticated successfully, take appropriate action
                                    NSLog(@"指紋認證成功");
                                    returnCode = @"1";
                                    
                                } else {
                                    // User did not authenticate successfully, look at error and take appropriate action
                                    NSLog(@"指紋認證失敗,%@",error.description);
                                    // 錯誤碼 error.code
                                    // -1: 連續三次指紋識別錯誤
                                    // -2: 在TouchID對話框中點擊了取消按鈕
                                    // -3: 在TouchID對話框中點擊了輸入密碼按鈕
                                    // -4: TouchID對話框被系統取消,例如按下Home或者電源鍵
                                    // -8: 連續五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統密碼
                                    
                                    returnCode = [@(error.code) stringValue];
                                    
                                    
                                }
                            }];
        
    } else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
        NSLog(@"TouchID設備不可用");
        // TouchID沒有設置指紋
        // 關閉密碼(系統如果沒有設置密碼TouchID無法啟用)
        
    }

上面就是獲取指紋驗證的方法,可以在成功和失敗的方法里面添加相應的邏輯


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM