【问题标题】:How to handle face id permission alert?如何处理人脸识别权限警报?
【发布时间】:2018-07-29 16:45:21
【问题描述】:

我写了如下代码sn-p

if ([contextNew canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error])
{
    [contextNew evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"",nil) reply:^(BOOL success, NSError *error)
    {
        if(success)
        {

        }
        else
        {

        }
    }
}

在验证人脸后进入成功阻止。我想处理人脸 id 权限警报。 我想获得 face id 的 Yes 或 permission grant 方法。 当我们得到像相机的 AVAuthorizationStatus 之类的东西时

【问题讨论】:

    标签: ios objective-c face-id localauthentication


    【解决方案1】:

    不确定,这是否能解决您的问题,但我有以下选项来检测 LAAuthentication 错误类型(在 Swift 中,您需要将其转换为 Objective-C)

    laContext.evaluatePolicy(biometricsPolicy, localizedReason: localizedReason, reply: { (isSuccess, error) in
        if let laError = error {
           // handle error
           self.showLAError(laError: laError)
        } else if isSuccess {
          // handle success
        }
    })
    
    
    // function to detect an error type
    func showLAError(laError: Error) -> Void {
        
        var message = ""
        
        switch laError {
            
        case LAError.appCancel:
            message = "Authentication was cancelled by application"
            
        case LAError.authenticationFailed:
            message = "The user failed to provide valid credentials"
            
        case LAError.invalidContext:
            message = "The context is invalid"
            
        case LAError.passcodeNotSet:
            message = "Passcode is not set on the device"
            
        case LAError.systemCancel:
            message = "Authentication was cancelled by the system"
            
        case LAError.touchIDLockout:
            message = "Too many failed attempts."
            
        case LAError.touchIDNotAvailable:
            message = "TouchID is not available on the device"
            
        case LAError.userCancel:
            message = "The user did cancel"
            
        case LAError.userFallback:
            message = "The user chose to use the fallback"
            
        default:
            
            if #available(iOS 11.0, *) {
                
                switch laError {
                    
                case LAError.biometryNotAvailable:
                    message = "Biometry is not available"
                    
                case LAError.biometryNotEnrolled:
                    message = "Authentication could not start, because biometry has no enrolled identities"
                    
                case LAError.biometryLockout:
                    message = "Biometry is locked. Use passcode."
    
                default:
                    message = "Did not find error code on LAError object"
                }
                
            }
            message = "Did not find error code on LAError object"
            
            
        }
        
        //return message
        print("LAError message - \(message)")
        
    }
    

    这里是用于 Objective-C 的所有类型的 LAError 列表

    编辑:

    如果您正在尝试使用 Simulator,请在此处查看如何测试 Face-Id:

    这是正在运行的 Objective-C 代码。试试这个看看:

    LAContext *laContext = [[LAContext alloc] init];
    
    NSError *error;
    
    if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        
        if (error != NULL) {
            // handle error
            //[self showError:error];
        } else {
            
            if (@available(iOS 11.0.1, *)) {
                if (laContext.biometryType == LABiometryTypeFaceID) {
                    //localizedReason = "Unlock using Face ID"
                    NSLog(@"FaceId support");
                } else if (laContext.biometryType == LABiometryTypeTouchID) {
                    //localizedReason = "Unlock using Touch ID"
                    NSLog(@"TouchId support");
                } else {
                    //localizedReason = "Unlock using Application Passcode"
                    NSLog(@"No Biometric support");
                }
            } else {
                // Fallback on earlier versions
            }
            
            
            [laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Test Reason" reply:^(BOOL success, NSError * _Nullable error) {
                
                if (error != NULL) {
                    // handle error
                } else if (success) {
                    // handle success response
                } else {
                    // handle false response
                }
            }];
        }
    }
    

    【讨论】:

    • 我已经添加了代码,但我希望弹出我已附加到问题的“是”按钮单击操作以获取人脸 ID 权限。当我们单击不允许时,以防 LAError .touchIDNotAvailable:但是当我们点击 Yes 时,如果用户成功验证人脸或 Touch ID 时它会进入成功块,它不会发生任何事情。我希望弹出 Face ID 权限的“是”按钮单击操作。请参阅随附的权限弹出图像。
    • @Pratik - 如果您尝试使用模拟器,请参阅编辑。
    • 我们正在尝试设备而不是模拟器。它在正确的面部认证后进入成功博客。
    • @Pratik 现在具体的问题是什么?它工作正常还是仍然(使用我的代码)显示相同的弹出消息?
    猜你喜欢
    • 2015-09-20
    • 2020-03-21
    • 2011-12-11
    • 1970-01-01
    • 2019-08-31
    • 2020-12-14
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多