If TouchID fails, forward to system passcode authentication
Asked Answered
A

2

13

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.

Atworth answered 9/11, 2017 at 11:53 Comment(0)
A
22

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.

Arnettaarnette answered 9/11, 2017 at 12:3 Comment(0)
G
21

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)!)
    }
}
Gardner answered 9/11, 2017 at 12:4 Comment(3)
if the passcode entered successfully then how to redirect to my app.Atworth
@Atworth 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.Gardner
@Gardner with this answer it will not ask for biometric authentication not even first time, it will directly ask to enter passcodeEyeglasses

© 2022 - 2024 — McMap. All rights reserved.