刀哥之指纹识别biometrics

指纹识别 - 生物识别

简介

  • 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(@"买了");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值