標籤:
簡單介紹:
支援系統和機型
iOS系統的指紋識別功能最低支援的機型為iPhone 5s,最低支援系統為iOS 8,雖然安裝iOS 7系統的5s機型可以使用系統提供的指紋解鎖功能,但由於API並未開放,所以理論上第三方軟體不可使用。
依賴架構
LocalAuthentication.framework
import <LocalAuthentication/LocalAuthentication.h>
注意事項
做iOS 8以下版本適配時,務必進行API驗證,避免調用相關API引起崩潰。
使用類
LAContext指紋驗證操作對象
代碼實現:
//初始化內容物件 LAContext* context = [[LAContext alloc] init]; //錯誤對象 NSError * error = nil; NSString * result = @"驗證"; context.localizedFallbackTitle = @"123"; //判斷裝置是否支援touchID BOOL isSupport = [context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]; if (isSupport) { //指紋識別函數 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"用 Touch ID 登入" reply:^(BOOL success, NSError *error) { //如果成功 if (success) { NSLog(@"驗證成功"); }else{ switch (error.code) { case LAErrorSystemCancel: { NSLog(@"Authentication was cancelled by the system"); //切換到其他APP,系統取消驗證Touch ID break; } case LAErrorUserCancel: { NSLog(@"Authentication was cancelled by the user"); //使用者取消驗證Touch ID break; } case LAErrorUserFallback: { NSLog(@"User selected to enter custom password"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //使用者選擇輸入密碼,切換主線程處理 }]; break; } } } }];} else { switch (error.code) { case LAErrorTouchIDNotEnrolled: { NSLog(@"TouchID is not enrolled"); break; } case LAErrorPasscodeNotSet: { NSLog(@"A passcode has not been set"); break; } default: { NSLog(@"TouchID not available"); break; } } NSLog(@"%@",error.localizedDescription);}
下面是LAError中每個枚舉對應的含義
typedef NS_ENUM(NSInteger, LAError){ //授權失敗 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed, //使用者取消Touch ID授權 LAErrorUserCancel = kLAErrorUserCancel, //使用者選擇輸入密碼 LAErrorUserFallback = kLAErrorUserFallback, //系統取消授權(例如其他APP切入) LAErrorSystemCancel = kLAErrorSystemCancel, //系統未設定密碼 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet, //裝置Touch ID不可用,例如未開啟 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable, //裝置Touch ID不可用,使用者未錄入 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,}
iOS Touch ID 使用詳情