If TouchID fails, forward to system passcode authentication

11,270

Solution 1

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.

Solution 2

Replace LAPolicy policy enum value deviceOwnerAuthenticationWithBiometrics with 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.

Try this and see:

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)!)
    }
}
Share:
11,270
saravanar
Author by

saravanar

Updated on June 08, 2022

Comments

  • saravanar
    saravanar about 2 years

    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.

  • saravanar
    saravanar over 6 years
    if the passcode entered successfully then how to redirect to my app.
  • Krunal
    Krunal over 6 years
    @saravanar you have alreary managed it using condition if success {. If passcode is corrent and passcode view will be dismissed automatically and gives you sucess as a boolean value, which you can use for further action.
  • Arshad Shaik
    Arshad Shaik almost 3 years
    @Krunal with this answer it will not ask for biometric authentication not even first time, it will directly ask to enter passcode