//1.判斷iOS8及以后的版本
if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
//從iPhone5S開始,出現指紋識別技術,所以說在此處可以進一步判斷是否是5S以后機型
//2.創建本地驗證上下文對象-->這里導入框架LocalAuthentication
LAContext *context = [LAContext new];
// 3.判斷能否使用指紋識別
//Evaluate: 評估
//Policy: 策略
//LAPolicyDeviceOwnerAuthenticationWithBiometrics: 設備擁有者授權 用 生物識別技術
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]){
//4.在可以使用的前提下就會調用
//localizedReason本地原因alert顯示
NSString *localizedReason = @"指紋登錄";
if (@available(iOS 11.0, *)) {
if (context.biometryType == LABiometryTypeTouchID) {
}else if (context.biometryType == LABiometryTypeFaceID){
localizedReason = @"人臉識別";
}
} else {
// Fallback on earlier versions
}
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:localizedReason reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
}else {
//不需要統計用戶取消
if(error.code != -2){
// 指紋識別Touch ID提供3+2 = 5次指紋識別機會----->3次識別失敗后,指紋驗證框消失(會報錯code = -1)然后點擊指紋會再次彈框可驗證兩次,如果五次指紋識別全部錯誤,就需要手動輸入數字密碼,數字密碼可以輸入6次,如果6次輸入密碼錯誤,系統停止驗證,等待驗證時間后會提供再次驗證的機會,正確及驗證成功(1次),錯誤則時間累加等待驗證,以此類推. (iOS10不一樣, 5次之后有問題: 需要進入設置中 -- TouchID與密碼, 輸入一次密碼, 就可以解開)
//Code=-2 "Canceled by user
//Code=-1 "Application retry limit exceeded."
//Code=-8 "Biometry is locked out."
// NSLog(@"error: %@", error);
//有的情況, 需要對錯誤的次數做累計, 此時就需要排除用戶取消
}
// switch (error.code)
// {
// case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials
// {
// NSLog(@"授權失敗"); // -1 連續三次指紋識別錯誤
// }
// break;
// case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button)
// {
// NSLog(@"用戶取消驗證Touch ID"); // -2 在TouchID對話框中點擊了取消按鈕
//
// }
// break;
// case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password)
// {
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSLog(@"用戶選擇輸入密碼,切換主線程處理"); // -3 在TouchID對話框中點擊了輸入密碼按鈕
// }];
//
// }
// break;
// case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground)
// {
// NSLog(@"取消授權,如其他應用切入,用戶自主"); // -4 TouchID對話框被系統取消,例如按下Home或者電源鍵
// }
// break;
// case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device.
//
// {
// NSLog(@"設備系統未設置密碼"); // -5
// }
// break;
// case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device
// {
// NSLog(@"設備未設置Touch ID"); // -6
// }
// break;
// case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers
// {
// NSLog(@"用戶未錄入指紋"); // -7
// }
// break;
//
//#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
// case LAErrorTouchIDLockout: //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 用戶連續多次進行Touch ID驗證失敗,Touch ID被鎖,需要用戶輸入密碼解鎖,先Touch ID驗證密碼
// {
// NSLog(@"Touch ID被鎖,需要用戶輸入密碼解鎖"); // -8 連續五次指紋識別錯誤,TouchID功能被鎖定,下一次需要輸入系統密碼
// }
// break;
// case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然來了電話,電話應用進入前台,APP被掛起啦");
// {
// NSLog(@"用戶不能控制情況下APP被掛起"); // -9
// }
// break;
// case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated.
// {
// NSLog(@"LAContext傳遞給這個調用之前已經失效"); // -10
// }
// break;
//#else
//#endif
// default:
// {
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSLog(@"其他情況,切換主線程處理");
// }];
// break;
// }
// }
}
}];
}else{
// NSLog(@"請確保(5S以上機型),TouchID未打開");
}
}else{
// NSLog(@"此設備不支持指紋識別"");
}