iOS FaceID 和 TouchID

一、TouchID

1.引入框架

#import 

2.判断是否支持TouchID

LAContext* context = [[LAContextalloc]init];
//错误对象
NSError* error =nil;
//首先使用canEvaluatePolicy判断设备支持状态
BOOL isSupport = [contextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricserror:&error]

3.开始识别,根据不同错误码提示

NSString*alertName =@"提示信息";
if (isSupport) {
        //支持指纹验证
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:alertName reply:^(BOOL success, NSError *error) {
            if (success) {
                //验证成功,主线程处理UI
                dispatch_queue_t mainQueue = dispatch_get_main_queue();
                dispatch_sync(mainQueue, ^{
                    self.label.text = @"验证成功";
                });
            }
            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);
    }```
#总结

二、FaceID

1.新增权限申请,导入框架

⚠️:不同于touchid,faceID需要在info.plist中申请权限
在info.plist文件里设置NSFaceIDUsageDescription就ok。这个可以理解为开启FaceID权限,就相当于调定位、相册那些权限一样。

#import 

2.判断是否支持

  NSString *message = @"面容 ID 短时间内失败多次,需要验证手机密码";
NSInteger deviceType = LAPolicyDeviceOwnerAuthenticationWithBiometrics;//单纯FaceID,LAPolicyDeviceOwnerAuthentication会有密码验证
    LAContext *laContext = [[LAContext alloc] init];
    laContext.localizedFallbackTitle = @""; // 隐藏左边的按钮(默认是忘记密码的按钮)
    NSError *error = nil;
    BOOL isSupport = [laContext canEvaluatePolicy:(deviceType) error:&error];

3.调用识别

if (isSupport) {
        [laContext evaluatePolicy:(deviceType) localizedReason:message reply:^(BOOL s, NSError * _Nullable error) {
            if (s) {
                success();
            }else {
                failure(error, LAContextErrorAuthorFailure);
            }
        }];
    }else {
        failure(error, LAContextErrorAuthorNotAccess);
    }
  1. 根据错误代码处理
error.code == -8 :超过FaceID尝试次数,已被锁
error.code == -7 :未开启FaceID权限
error.code == -6 :该手机不支持FaceID

你可能感兴趣的:(iOS FaceID 和 TouchID)