標籤:ios 指紋識別
1. 簡介
- iPhone 5S 開始支援
- iOS 8.0 開放了 Touch ID 的介面
2. 代碼準備
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self inputUserinfo];}/// 輸入使用者資訊- (void)inputUserinfo { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"購買" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"購買", nil]; alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; [alertView show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"%zd", buttonIndex); if (buttonIndex == 0) { return; } UITextField *usernameText = [alertView textFieldAtIndex:0]; UITextField *pwdText = [alertView textFieldAtIndex:1]; if ([usernameText.text isEqualToString:@"zhang"] && [pwdText.text isEqualToString:@"123"]) { [self purchase]; } else { [[[UIAlertView alloc] initWithTitle:@"提示" message:@"使用者名稱或密碼錯誤" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil] show]; }}/// 購買- (void)purchase { NSLog(@"購買");}
3. 指紋識別標頭檔
#import <LocalAuthentication/LocalAuthentication.h>
判斷是否支援指紋識別
// 檢查版本if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) { [self inputUserinfo]; return;}// 檢查是否支援指紋識別LAContext *ctx = [[LAContext alloc] init];if ([ctx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:NULL]) { NSLog(@"支援指紋識別"); // 非同步輸入指紋 [ctx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"購買" reply:^(BOOL success, NSError *error) { NSLog(@"%d %@ %@", success, error, [NSThread currentThread]); if (success) { [self purchase]; } else if (error.code == LAErrorUserFallback) { dispatch_async(dispatch_get_main_queue(), ^{ [self inputUserinfo]; }); } }]; NSLog(@"come here");} else { [self inputUserinfo];}
4. 錯誤代號
| 錯誤 |
描述 |
| LAErrorAuthenticationFailed |
指紋無法識別 |
| LAErrorUserCancel |
使用者點擊了”取消”按鈕 |
| LAErrorUserFallback |
使用者取消,點擊了”輸入密碼”按鈕 |
| LAErrorSystemCancel |
系統取消,例如啟用了其他應用程式 |
| LAErrorPasscodeNotSet |
驗證無法啟動,因為裝置上沒有設定密碼 |
| LAErrorTouchIDNotAvailable |
驗證無法啟動,因為裝置上沒有 Touch ID |
| LAErrorTouchIDNotEnrolled |
驗證無法啟動,因為沒有輸入指紋 |
IOS開發-指紋識別