• 指纹解锁


    #import "FingerprintUnlockController.h"
    #import <LocalAuthentication/LocalAuthentication.h>
    
    @interface FingerprintUnlockController ()
    
    @end
    
    @implementation FingerprintUnlockController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
        self.view.backgroundColor = [UIColor whiteColor];
        
        [self authenticateUser];
    }
    
    // 指纹验证的实现
    - (void)authenticateUser
    {
        //初始化上下文对象
        LAContext* context = [[LAContext alloc] init];
        //错误对象
        NSError* error = nil;
        NSString* result = @"Authentication is needed to access your notes.";
        
        //首先使用canEvaluatePolicy 判断设备支持状态
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            //支持指纹验证
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
                if (success) {
                    //验证成功,主线程处理UI
                }
                else
                {
                    NSLog(@"%@",error.localizedDescription);
                    switch (error.code) {
                        case LAErrorSystemCancel:
                        {
                            NSLog(@"Authentication was cancelled by the system");
                            //切换到其他APP,系统取消验证Touch ID
                            break;
                        }
                        case LAErrorUserCancel:
                        {
                            NSLog(@"Authentication was cancelled by the user");
                            //用户取消验证Touch ID
                            break;
                        }
                        case LAErrorUserFallback:
                        {
                            NSLog(@"User selected to enter custom password");
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                //用户选择其他验证方式,切换主线程处理
                            }];
                            break;
                        }
                        default:
                        {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                //其他情况,切换主线程处理
                            }];
                            break;
                        }
                    }
                }
            }];
        }
        else
        {
            //不支持指纹识别,LOG出错误详情
            
            switch (error.code) {
                case LAErrorTouchIDNotEnrolled:
                {
                    NSLog(@"TouchID is not enrolled");
                    break;
                }
                case LAErrorPasscodeNotSet:
                {
                    NSLog(@"A passcode has not been set");
                    break;
                }
                default:
                {
                    NSLog(@"TouchID not available");
                    break;
                }
            }
            
            NSLog(@"%@",error.localizedDescription);
    //        [self showPasswordAlert];
        }  }
    
    // 对几种情况的说明
    //typedef NS_ENUM(NSInteger, LAError)
    //{
    //    //授权失败
    //    LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
    //    
    //    //用户取消Touch ID授权
    //    LAErrorUserCancel           = kLAErrorUserCancel,
    //    
    //    //用户选择输入密码
    //    LAErrorUserFallback         = kLAErrorUserFallback,
    //    
    //    //系统取消授权(例如其他APP切入)
    //    LAErrorSystemCancel         = kLAErrorSystemCancel,
    //    
    //    //系统未设置密码
    //    LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,
    //    
    //    //设备Touch ID不可用,例如未打开
    //    
    //    LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,
    //    
    //    //设备Touch ID不可用,用户未录入
    //    LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,
    //} NS_ENUM_AVAILABLE(10_10, 8_0);
    
    @end
  • 相关阅读:
    IOC架构设计之ButterKnife源码&原理(二)下篇
    IOC架构设计之ButterKnife源码&原理(二)中篇
    IOC架构设计之ButterKnife源码&原理(二)上篇
    IOC架构设计之控制反转和依赖注入(一)
    RXJava之线程控制Scheduler(四)
    RXJava之变换(三)
    RXJava之扩展的观察者模式(二)
    微信小程序弹框提示绑定手环实例
    SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源
    SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程
  • 原文地址:https://www.cnblogs.com/pengyunjing/p/5967361.html
Copyright © 2020-2023  润新知