前言
NS_CLASS_AVAILABLE(10_10, 8_0) @interface LAContext : NSObject
-
指紋識別功能是 iPhone 5s 推出的,SDK 是 iOS 8.0 推出。
-
推出指紋識別的主要原因是為了簡化支付,移動支付的環節越簡單越好。
-
1)指紋驗證方式:
// 只使用指紋驗證 LAPolicyDeviceOwnerAuthenticationWithBiometrics NS_ENUM_AVAILABLE(NA, 8_0) = kLAPolicyDeviceOwnerAuthenticationWithBiometrics, // 使用指紋和設備密碼驗證,指紋錯誤或無法驗證時會自動跳轉到輸入密碼驗證界面 LAPolicyDeviceOwnerAuthentication NS_ENUM_AVAILABLE(10_11, 9_0) = kLAPolicyDeviceOwnerAuthentication
-
2)指紋驗證錯誤信息:
// 指紋無法識別 error.code == -1 LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed, // 用戶點擊了 "取消" 按鈕 error.code == -2 LAErrorUserCancel = kLAErrorUserCancel, // 用戶取消,點擊了 "輸入密碼" 按鈕 error.code == -3 LAErrorUserFallback = kLAErrorUserFallback, // 系統取消,例如激活了其他應用程序 error.code == -4 LAErrorSystemCancel = kLAErrorSystemCancel, // 驗證無法啟動,因為設備上沒有設置密碼 error.code == -5 LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet, // 驗證無法啟動,因為設備上沒有 Touch ID error.code == -6 LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable, // 驗證無法啟動,因為沒有輸入指紋 error.code == -7 LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled, LAErrorTouchIDLockout NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorTouchIDLockout, LAErrorAppCancel NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorAppCancel, LAErrorInvalidContext NS_ENUM_AVAILABLE(10_11, 9_0) = kLAErrorInvalidContext
1、指紋識別的使用
-
Objective-C
// 包含本地身份驗證頭文件 #import <LocalAuthentication/LocalAuthentication.h> // 判斷用戶手機系統是否是 iOS 8.0 以上版本 if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) { return; } // 實例化本地身份驗證上下文 LAContext *context= [[LAContext alloc] init]; // 判斷是否支持指紋識別 if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:NULL]) { return; } [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"請驗證已有指紋" reply:^(BOOL success, NSError * _Nullable error) { // 輸入指紋開始驗證,異步執行 if (success) { [self refreshUI:[NSString stringWithFormat:@"指紋驗證成功"] message:nil]; } else { [self refreshUI:[NSString stringWithFormat:@"指紋驗證失敗"] message:error.userInfo[NSLocalizedDescriptionKey]]; } }]; // 主線程刷新 UI - (void)refreshUI:(NSString *)str message:(NSString *)msg { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:msg preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alert animated:YES completion:^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [alert dismissViewControllerAnimated:YES completion:nil]; }); }]; }); }
-
效果
2、指紋識別的設置
-
Objective-C
// 判斷是否支持指紋識別 if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) { } // 取消指紋驗證 [context invalidate]; // 設置 輸入密碼 按鈕的標題 context.localizedFallbackTitle = @"輸入密碼按鈕標題"; // 設置 取消 按鈕的標題 context.localizedCancelTitle = @"取消按鈕標題";