用户取消了touchID实施错误

时间:2019-09-17 17:27:13

标签: ios swift biometrics touch-id localauthentication

如何解决TouchId错误:Domain=com.apple.LocalAuthentication Code=-2 "Canceled by user."

我尝试再次添加本地上下文:

let myContext = LAContext()
let myLocalizedReasonString = "Please use your last login for Inspyrus Supplier Central."        

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
            DispatchQueue.main.async {
                if success {
                    self.btnLoginClicked(UIButton())
                } else {
                    print(evaluateError?.localizedDescription ?? "Failed to authenticate")
                    // Fall back to a asking for username and password.
                    // ...
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以检查从evaluateError调用返回的evaluatePolicy是否是LAError.userCancel

类似这样的东西:

if success {
    //...
}
else if let authError = evaluateError as? LAError {
    switch authError.code {
    case .userCancel:
        // Authentication was canceled by user (e.g. tapped Cancel button).
        break
    default:
        // Other error
        break
    }

    // Or
    switch authError {
    case LAError.userCancel:
        // Authentication was canceled by user (e.g. tapped Cancel button).
        break
    default:
        // Other error
        break
    }
}