如果 TouchID 失败,则转发到系统密码验证

If TouchID fails, forward to system passcode authentication

我想使用 TouchID 验证我的应用程序,验证成功。如果 TouchID 不匹配,则会打开“再试一次”警报,并且在该警报中有“输入密码”选项。如果用户选择它,系统密码验证应该显示,但我该怎么做?

在这里分享我的代码:

func touchIDAuthentication() {
    let context = LAContext() //1
    var error:NSError?
    guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
        showAlertViewIfNoBiometricSensorHasBeenDetected()
        return
    }
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &errorPointer) {
        context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply: { (success, error) in
            if success {
                DispatchQueue.main.async {
                    print("Authentication was successful")
                }
            }else {
                DispatchQueue.main.async {
                    self.displayErrorMessage(error: error as! LAError )
                    print("Authentication was error")
                }
            }
        })
    }else {
        self.showAlertWith(title: "Error", message: (errorPointer?.localizedDescription)!)
    }
}



func displayErrorMessage(error:LAError) {
        var message = ""
        switch error.code {
        case LAError.authenticationFailed:
            message = "Authentication Failed."
            break
        case LAError.userCancel:
            message = "User Cancelled."
            break
        case LAError.userFallback:
            message = "Fallback authentication mechanism selected."
            break
        case LAError.touchIDNotEnrolled:
            message = "Touch ID is not enrolled."

        case LAError.passcodeNotSet:
            message = "Passcode is not set on the device."
            break
        case LAError.systemCancel:
            message = "System Cancelled."
            break
        default:
            message = error.localizedDescription
        }
        self.showAlertWith(title: "Authentication Failed", message: message)
    }

如果输入移动到我的应用程序中的密码,如何显示此屏幕。如何实现这一点帮助我。提前致谢。

如果您使用策略 .deviceOwnerAuthentication,则会立即显示 "Enter password" 选项。

如果您按原样使用 .deviceOwnerAuthenticationWithBiometrics,则 "Enter Password" 选项仅在第一次生物识别身份验证尝试失败后才会显示。

无论用户如何验证,您的完成闭包都会被调用。

LAPolicy 策略枚举值 deviceOwnerAuthenticationWithBiometrics 替换为 deviceOwnerAuthentication

Note: If user has enable biometric (face id or touch id) authentication, then device will ask first for biometric authentication and if user choose fall back authentication, then only deviceOwnerAuthentication will show passcode screen.

试试看:

func touchIDAuthentication() {
    let context = LAContext()
    var error:NSError?

    // edit line - deviceOwnerAuthentication
    guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else {
        //showAlertViewIfNoBiometricSensorHasBeenDetected()
        return
    }

    // edit line - deviceOwnerAuthentication
    if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &errorPointer) {

        // edit line - deviceOwnerAuthentication
        context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason, reply: { (success, error) in
            if success {
                DispatchQueue.main.async {
                    print("Authentication was successful")
                }
            }else {
                DispatchQueue.main.async {
                    //self.displayErrorMessage(error: error as! LAError )
                    print("Authentication was error")
                }
            }
        })
    }else {
       // self.showAlertWith(title: "Error", message: (errorPointer?.localizedDescription)!)
    }
}