ios - touchID 实现错误已被用户取消

标签 ios swift biometrics touch-id localauthentication

如何解决 TouchId 错误:Domain=com.apple.LocalAuthentication Code=-2“已被用户取消。”

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

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.
                    // ...
                }
            }
        }
    }
}

最佳答案

您可以检查从 evaluatePolicy 调用返回的 evaluateError 是否为 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
    }
}

关于ios - touchID 实现错误已被用户取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57979271/

相关文章:

ios - 委托(delegate)返回零

ios - Swift:子类 MKPolyline

ios - 设置可重用 header 时出错 : 'Could not load NIB in bundle

ios - Swift Storyboard - 水平居中图标和多行文本

cygwin -\r'' 中意外标记 `$' 附近出现语法错误

xml - 使用 Accord.NET 解析 HaarCascade XML - 节点类型不支持 ReadElementContentAsString 方法 None

perl - 为什么 Perl 在生物学研究中被如此广泛地使用?

ios - iphone 到 ipad 转换 : some storyboard options missing

ios - AVPlayer中如何处理网络中断

ios - 如何在应用程序中保存用户名和密码,以便我们不需要登录应用程序?