指紋識別 - 生物識別
簡介
- iPhone 5S 開始支持
- iOS 8.0 開放了 Touch ID 的接口
代碼準備
- (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(@"購買");
}
指紋識別
頭文件
#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];
}
錯誤代號
錯誤 |
描述 |
LAErrorAuthenticationFailed |
指紋無法識別 |
LAErrorUserCancel |
用戶點擊了”取消”按鈕 |
LAErrorUserFallback |
用戶取消,點擊了”輸入密碼”按鈕 |
LAErrorSystemCancel |
系統取消,例如激活了其他應用程序 |
LAErrorPasscodeNotSet |
驗證無法啓動,因爲設備上沒有設置密碼 |
LAErrorTouchIDNotAvailable |
驗證無法啓動,因爲設備上沒有 Touch ID |
LAErrorTouchIDNotEnrolled |
驗證無法啓動,因爲沒有輸入指紋 |
使用 UIAlertController
- (void)inputUserInfo {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"請輸入用戶名和口令" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入用戶名";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"請輸入用戶密碼";
textField.secureTextEntry = YES;
}];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"購買" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSString *username = [alert.textFields[0] text];
NSString *pwd = [alert.textFields[1] text];
if ([username isEqualToString:@"zhang"] && [pwd isEqualToString:@"1"]) {
[self purchase];
} else {
[self showErrorMsg];
}
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)showErrorMsg {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用戶名或密碼不正確" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)purchase {
NSLog(@"買了");
}