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(@"买了");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,558评论 4 61
  • 2017年9月12日,苹果发布了新一代的 iPhone 8 和 iPhone X,iPhone 手机已在不知不觉中...
    40c0490e5268阅读 9,031评论 6 34
  • 新的一年开始了,估计大家已经许下了心愿,不知道是否记得呢?在新的一年里,大家到底有什么期许呢?今天跟着小编一起去了...
    星座有预言阅读 2,880评论 0 0
  • 愿你每天都开心,没人强迫你去做你不喜欢的事,这一天做的事情都顺心,早起占到了你最喜欢的座位,早餐吃到了最喜欢吃的早...
    组图图解部王立宏阅读 2,047评论 0 1
  • gRPC 简介: gRPC 是一款高性能、开源的 RPC 框架,产自 Google,基于 ProtoBuf 序列化...
    谢烟客阅读 13,567评论 6 36

友情链接更多精彩内容