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

友情链接更多精彩内容