iOS开发_apple面容、指纹验证使用

1、导入包

#import <LocalAuthentication/LocalAuthentication.h>

2、使用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    LAContext *lacontext = [[LAContext alloc] init];
    BOOL support = [lacontext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:NULL];
    if(@available(iOS 11.0, *)) {
        if([lacontext respondsToSelector:@selector(biometryType)]){
            if(lacontext.biometryType == LABiometryTypeFaceID) {
                CHLog(@"面容ID登录");
                // LAPolicyDeviceOwnerAuthenticationWithBiometrics:
                [lacontext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                          localizedReason:@"面容ID登录..."
                                    reply:^(BOOL success, NSError * _Nullable error) {
                    if (success) {
                        
                    }
                    else {
                        LAError code = error.code;
                        switch (code) {
                            case LAErrorAuthenticationFailed:
                                CHLog(@"身份验证失败");
                                break;
                            case LAErrorUserCancel:
                                CHLog(@"用户在认证时点击取消");
                                break;
                            case LAErrorUserFallback:
                                CHLog(@"用户点击输入密码取消指纹/面容验证");
                                break;
                            case LAErrorSystemCancel:
                                CHLog(@"身份认证被系统取消(按下Home键或电源键)");
                                break;
                            case LAErrorPasscodeNotSet:
                                CHLog(@"设备未设置密码");
                                break;
                            case LAErrorTouchIDNotAvailable:
                                CHLog(@"该设备指纹验证不可用");
                                break;
                            case LAErrorTouchIDNotEnrolled:
                                CHLog(@"用户未录入指纹");
                                break;
                            case LAErrorTouchIDLockout:
                                CHLog(@"连续五次密码错误,FaceID被锁定.");
                                break;
                            case LAErrorAppCancel:
                                CHLog(@"用户不能控制情况下App被挂起.");
                                break;
                            default:
                                break;
                        }
                    }
                }];
            }
            else if(lacontext.biometryType == LABiometryTypeTouchID) {
                CHLog(@"指纹登录");
            }
            else {
                CHLog(@"未授权");
            }
        }
    }
}
posted @ 2020-07-11 13:15  CH520  阅读(415)  评论(0编辑  收藏  举报