简体   繁体   English

用户拒绝使用时无法检查面容 ID

[英]Can't check Face ID when user denied to use

I want to take different actions to users if device support Face ID or Touch ID .如果设备支持Face IDTouch ID,我想对用户采取不同的操作。

When using the Face ID , iOS asking permission to use.使用Face ID 时,iOS 会询问使用权限。 (unlike Touch ID). (与触控 ID 不同)。

And if the user denies permission, context.biometryType return LABiometryTypeNone.如果用户拒绝许可,context.biometryType 返回 LABiometryTypeNone。

Is there anyway to check Touch ID or Face ID supported by device .无论如何检查设备支持的Touch IDFace ID

LAContext *context = [[LAContext alloc] init];

NSError *error;

if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

}

if (@available(iOS 11.0, *)) {
    if (context.biometryType == LABiometryTypeFaceID) {
        // support FaceID 
    }
}

// support TouchID

Console output控制台输出

(lldb) po error
Error Domain=com.apple.LocalAuthentication Code=-6 "User has denied the use of biometry for this app." UserInfo={NSLocalizedDescription=User has denied the use of biometry for this app.}

(lldb) po context.biometryType
LABiometryTypeNone

NOTE: I Don't want to use passcode authentication.注意:我不想使用密码验证。 I just need to know device is support Touch ID or Face ID我只需要知道设备是否支持 Touch ID 或 Face ID

否。如果用户拒绝的隐私权限提示与NSFaceIDUsageDescription ,所有未来实例LAContext将有biometryType财产.none一次canEvalutePolicy:error:叫他们。

It's too late but I hope this can help whoever have the same issue.为时已晚,但我希望这可以帮助遇到相同问题的人。

LAContext().canEvaluatePolicy(. deviceOwnerAuthentication , error: nil) LAContext().canEvaluatePolicy(. deviceOwnerAuthenticationWithBiometrics , error: nil) LAContext().canEvaluatePolicy( .deviceOwnerAuthentication , error: nil) LAContext().canEvaluatePolicy( .deviceOwnerAuthenticationWithBiometrics , error: nil)

the difference between deviceOwnerAuthentication & deviceOwnerAuthenticationWithBiometrics that the first one will tell you the device have authentication method or not .. second one have same behavior but only works is user accepted the permission. deviceOwnerAuthenticationdeviceOwnerAuthenticationWithBiometrics之间的区别是,第一个会告诉您设备是否具有身份验证方法。第二个具有相同的行为,但只有用户接受了权限才有效。

enum BiometryResult: Int {
case faceID
case touchID
case notExist
}
class func biometryType() -> BiometryResult {
    let context = LAContext()
    if (context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)) {
        if (context.biometryType == LABiometryType.faceID) {
            return .faceID
        } else if (context.biometryType == LABiometryType.touchID) {
            return .touchID
        } else {
            return .notExist
        }
    }
    return .notExist
}

Use property biometryType of LAContext to check and evaluate available biometric policy.使用属性biometryTypeLAContext检查和评估提供生物识别的策略。 (For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication ) (对于密码验证,当生物识别失败时,使用: LAPolicyDeviceOwnerAuthentication

Try this and see:试试这个,看看:

LAContext *laContext = [[LAContext alloc] init];

NSError *error;


// For a passcode authentication , when biometric fails, use: LAPolicyDeviceOwnerAuthentication
//if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthentication error:&error]) {
if ([laContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {    
    if (error != NULL) {
        // handle error
    } else {

        if (@available(iOS 11, *)) {
            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 or Denied 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
            }
        }];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM