如何处理面部id权限警报?

时间:2018-02-19 12:33:50

标签: ios objective-c face-id localauthentication

我写了以下代码片段

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

        }
        else
        {

        }
    }
}
验证面部后它进入成功阻止。我想处理面部识别权限警报。 我想获得面部ID的Yes或权限授予方法。 因为我们得到像AVAuthorizationStatus的相机

This is the permission popup

1 个答案:

答案 0 :(得分: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类型的列表

编辑:

如果您正在尝试使用模拟器,请参阅此处如何测试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
            }
        }];
    }
}