美文网首页
【iOS】Touch ID 和Face ID

【iOS】Touch ID 和Face ID

作者: 努力奋斗的绝望者 | 来源:发表于2018-11-13 15:36 被阅读21次

Touch ID 和Face ID识别

- (void)TouchIDOrFaceID {
    LAContext *context = [[LAContext alloc] init];
    
    if ([self isSupport:context]) {
        //提示语
        NSString *localizedReason = @"指纹登录";
        if (@available(iOS 11.0, *)) {
            if (context.biometryType == LABiometryTypeFaceID){
                localizedReason = @"人脸识别";
            }
        }
        
        //TouchID/FaceID识别
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:localizedReason reply:^(BOOL success, NSError * _Nullable error) {
            
            if (success) {
                NSLog(@"TouchID/FaceID识别成功");
            }else {
                switch (error.code) {
                    case LAErrorAuthenticationFailed: // -1 连续三次TouchID识别错误
                    {
                        NSLog(@"授权失败");
                    }
                        break;
                    case LAErrorUserCancel: // -2 点击了取消按钮
                    {
                        NSLog(@"用户取消验证");
                    }
                        break;
                    case LAErrorUserFallback: // -3 点击了输入密码按钮
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用户选择输入密码,切换主线程处理");
                        }];
                    }
                        break;
                    case LAErrorSystemCancel: // -4 被系统取消,例如按下Home或者电源键
                    {
                        NSLog(@"取消授权,如其他应用切入,用户自主");
                    }
                        break;
                    case LAErrorPasscodeNotSet: // -5 设备系统未设置密码
                    {
                        NSLog(@"设备系统未设置密码");
                    }
                        break;
                    case LAErrorTouchIDNotAvailable: // -6 设备未设置TouchID
                    {
                        NSLog(@"设备未设置TouchID");
                    }
                        break;
                    case LAErrorTouchIDNotEnrolled: // -7 用户未录入TouchID
                    {
                        NSLog(@"用户未录入指纹");
                    }
                        break;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
                    case LAErrorTouchIDLockout: // -8 连续五次指纹识别错误,TouchID功能被锁定,下一次需要输入系统密码
                    {
                        NSLog(@"Touch ID被锁,需要用户输入密码解锁");
                    }
                        break;
                        
                    case LAErrorAppCancel: // -9 如突然来了电话,电话应用进入前台,APP被挂起啦");
                    {
                        NSLog(@"用户不能控制情况下APP被挂起");
                    }
                        break;
                        
                    case LAErrorInvalidContext: // -10 LAContext传递给这个调用之前已经失效
                    {
                        NSLog(@"LAContext传递给这个调用之前已经失效");
                    }
                        break;
#else
                        
#endif
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情况,切换主线程处理");
                        }];
                        break;
                    }
                }
            }
        }];
    }
}

//是否支持
- (BOOL)isSupport:(LAContext *)context {
    if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
        //能否使用TouchID/FaceID识别
        if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
            return YES;
        }else {
            NSLog(@"请确保(5S以上机型),TouchID未打开");
            return NO;
        }
    }else {
        NSLog(@"此设备不支持TouchID/FaceID识别");
        return NO;
    }
}

相关文章

网友评论

      本文标题:【iOS】Touch ID 和Face ID

      本文链接:https://www.haomeiwen.com/subject/gcrlfqtx.html