iOS 指紋解鎖 驗證TouchID,iostouchid
iOS指紋解鎖
1、首先,引入依賴架構 LocalAuthentication.framework
#import <LocalAuthentication/LocalAuthentication.h>
2、然後,判斷系統是否為iOS8及以上
//iOS8.0後才支援指紋識別介面 if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) { return; }
3、最後,在APP啟動時調用以下方法即可完成指紋解鎖的全部功能整合
- (void)evaluateAuthenticate{ //建立LAContext LAContext* context = [[LAContext alloc] init]; NSError* error = nil; NSString* result = @"請驗證已有指紋"; //首先使用canEvaluatePolicy 判斷裝置支援狀態 if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { //支援指紋驗證 [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) { if (success) { //驗證成功,主線程處理UI } else { NSLog(@"%@",error.localizedDescription); switch (error.code) { case LAErrorSystemCancel: { //系統取消授權,如其他APP切入 break; } case LAErrorUserCancel: { //使用者取消驗證Touch ID break; } case LAErrorAuthenticationFailed: { //授權失敗 break; } case LAErrorPasscodeNotSet: { //系統未設定密碼 break; } case LAErrorTouchIDNotAvailable: { //裝置Touch ID不可用,例如未開啟 break; } case LAErrorTouchIDNotEnrolled: { //裝置Touch ID不可用,使用者未錄入 break; } case LAErrorUserFallback: { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //使用者選擇輸入密碼,切換主線程處理 }]; break; } default: { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其他情況,切換主線程處理 }]; break; } } } }]; } else { //不支援指紋識別,LOG出錯誤詳情 NSLog(@"不支援指紋識別"); 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); }}