iOS8后蘋果開放了Touch ID的API給開發者,這也給我們的app帶來了新的體驗。開發者們可使用向第三方應用開放了Touch ID權限的API,以便他們在應用中使用指紋認證來完成用戶認證或支付購買。本文主要介紹如何在應用中集成Touch ID來校驗用戶的身份和注意事項。
支持系統和機型
iOS系統的指紋識別功能最低支持的機型為iPhone 5s,最低支持系統為iOS 8,雖然安裝iOS 7系統的5s機型可以使用系統提供的指紋解鎖功能,但由於API並未開放,所以理論上第三方軟件不可使用。
依賴框架
在使用前我們需要導入 LocalAuthentication.framework 這個庫

這個庫必須要Xcode6並且連接的是真機,才不會提示找不到的錯誤。 如果是模擬器會提示找不到這個庫。
注意事項
做iOS 8以下版本適配時,務必進行API驗證,避免調用相關API引起崩潰。
使用類
LAContext 指紋驗證操作對象
代碼
- (void)authenticateButtonTapped{ LAContext *context = [[LAContext alloc] init]; context.localizedFallbackTitle = @"輸入密碼"; NSError *error = nil; if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) { [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"您是這設備的所有者嗎?" reply:^(BOOL success, NSError *error) { if (success) { dispatch_async (dispatch_get_main_queue(), ^{ //在主線程更新 UI,不然會卡主 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"你是設備主人。" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }); }else{ /* // 用戶未提供有效證書,(3次機會失敗 --身份驗證失敗)。 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed, // 認證被取消,(用戶點擊取消按鈕)。 LAErrorUserCancel = kLAErrorUserCancel, // 認證被取消,用戶點擊回退按鈕(輸入密碼)。 LAErrorUserFallback = kLAErrorUserFallback, // 身份驗證被系統取消,(比如另一個應用程序去前台,切換到其他 APP)。 LAErrorSystemCancel = kLAErrorSystemCancel, // 身份驗證無法啟動,因為密碼在設備上沒有設置。 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet, // 身份驗證無法啟動,因為觸摸ID在設備上不可用。 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable, // 身份驗證無法啟動,因為沒有登記的手指觸摸ID。 沒有設置指紋密碼時。 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled, **/ switch (error.code) { case LAErrorAuthenticationFailed: NSLog(@"身份驗證失敗。"); break; case LAErrorUserCancel: NSLog(@"用戶點擊取消按鈕。"); break; case LAErrorUserFallback: { NSLog(@"用戶點擊輸入密碼。"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //用戶選擇輸入密碼,切換主線程處理 }]; break; } case LAErrorSystemCancel: NSLog(@"另一個應用程序去前台"); break; case LAErrorPasscodeNotSet: NSLog(@"密碼在設備上沒有設置"); break; case LAErrorTouchIDNotAvailable: NSLog(@"觸摸ID在設備上不可用"); break; case LAErrorTouchIDNotEnrolled: NSLog(@"沒有登記的手指觸摸ID。"); break; default: { NSLog(@"Touch ID沒配置"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{ //其他情況,切換主線程處理 }]; break; } } } }]; } else { dispatch_async (dispatch_get_main_queue(), ^{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤提示" message:@"您的設備沒有觸摸ID." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }); } }
其中:
(1)localizedFallbackTitle:用於設置左邊的按鈕的名稱,默認是輸入密碼。
(2)localizedReason:用於設置提示語,表示為什么要使用Touch ID,如代碼中@"您是這設備的所有者嗎?"。
操作流程
首先判斷系統版本,iOS8及以上版本執行-(void)authenticateButtonTapped方法,方法自動判斷設備是否支持和開啟Touch ID。
iOS 9
iOS 9加入了三種新的錯誤類型。
/// Authentication was not successful, because there were too many failed Touch ID attempts and /// Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating /// LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite. LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout, /// Authentication was canceled by application (e.g. invalidate was called while /// authentication was in progress). LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel, /// LAContext passed to this call has been previously invalidated. LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
其中,
LAErrorTouchIDLockout是在8.0中也會出現的情況,但並未歸為單獨的錯誤類型,這個錯誤出現,源自用戶多次連續使用Touch ID失敗,Touch ID被鎖,需要用戶輸入密碼解鎖,這個錯誤的交互LocalAuthentication.framework已經封裝好了,不需要開發者關心。
LAErrorAppCancel和LAErrorSystemCancel相似,都是當前軟件被掛起取消了授權,但是前者是用戶不能控制的掛起,例如突然來了電話,電話應用進入前台,APP被掛起。后者是用戶自己切到了別的應用,例如按home鍵掛起。
LAErrorInvalidContext很好理解,就是授權過程中,LAContext對象被釋放掉了,造成的授權失敗
官方文檔就是這么短短幾句話!!!
