Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If TouchID fails, forward to system passcode authentication

I want to use TouchID authenticate my app, authentication worked successfully. If TouchID does not match, then the Try Again alert opens, and in that alert is the Enter Password option. If the user selects that, the system passcode authentication should display, but how can I do that?

Here share my code:

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)
    }

How to show this screen if enter the passcode it move into my app. How achieve this help me. Thanks advance.

like image 664
saravanar Avatar asked Nov 09 '17 11:11

saravanar


1 Answers

If you use policy .deviceOwnerAuthentication then the "Enter password" option is displayed immediately.

If you use .deviceOwnerAuthenticationWithBiometrics, as you are, then the "Enter Password" option is only shown after the first unsuccessful biometric authentication attempt.

Regardless of how the user authenticates, your completion closure will be called.

like image 56
Paulw11 Avatar answered Nov 06 '22 01:11

Paulw11