码迷,mamicode.com
首页 > 其他好文 > 详细

刀哥之指纹识别biometrics

时间:2015-08-19 20:25:29      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:ios   指纹识别   

指纹识别 - 生物识别

简介

  • 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(@"买了");
}

刀哥之指纹识别biometrics

标签:ios   指纹识别   

原文地址:http://blog.csdn.net/jiahao8915/article/details/47786377

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!