iOS LocalAuthentication(指纹识别)

指纹识别
#import "ViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1. 判断系统版本
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        //可以使用指纹识别 --> 5S 以后的机型
        
        //2. 判断时候可以使用指纹识别功能
        
        //2.1 创建 LA 对象上下文
        LAContext *context = [[LAContext alloc] init];
        
        //2.2 判断是否能否使用
        //Evaluate: 评估
        //Policy: 策略
        //LAPolicyDeviceOwnerAuthenticationWithBiometrics : 可以使用指纹识别技术
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            // 可以使用
            
            //3. 开始启用指纹识别
            //localizedReason : 显示在界面上的原因, 一般用于提示用户, 为什么要使用此功能
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"请验证指纹, 以打开高级隐藏功能" reply:^(BOOL success, NSError * _Nullable error) {
                
                //判断是否成功
                if (success) {
                    
                    
                    //4. 指纹识别在更新 UI 时, 一定要注意, 在主线程更新
                    
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"这是标题" message:@"你成功了" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
                        
                        [alert show];
                    });
                    
                } else {
                    
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"这是标题" message:@"验证失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
                        
                        [alert show];
                        
                    });
                }
                
                // 判断错误 如果需要区分不同的错误来提示用于, 必须判断 error, -1, -2, -8
                if (error) {
                    NSLog(@"error Code: %ld",error.code);
                    NSLog(@"error : %@",error.userInfo);
                }
                
            }];
            
        } else {
            NSLog(@"对不起, 5S 以上机型才能使用此功能, 请卖肾后重拾");
        }
        
    } else {
        NSLog(@"对不起, 系统版本过低");
    }
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值