Touch ID - How to get to the passcode screen? -Swift

JakeC picture JakeC · Dec 9, 2015 · Viewed 8.4k times · Source

So I have been searching all over stackoverflow and I cannot seen to find the answer for this. How do I allow a user to access the built in apple passcode screen when a user taps "Enter Password"? Please click the link provided below to see an image illustrations this issue. Thanks for the help and please write it in Swift!

http://i.stack.imgur.com/id9E3.jpg

Answer

parag picture parag · Jul 15, 2016

This can be done with iOS 9, which exposes a new LAPolicy type, i.e. when calling evaluatePolicy, pass in DeviceOwnerAuthentication and not DeviceOwnerAuthenticationWithBiometrics.

Here's some swift:

import LocalAuthentication

function authenticateUser() {
    let context = LAContext()

    context.evaluatePolicy(LAPolicy.DeviceOwnerAuthentication, localizedReason: "Please authenticate to proceed.") { [weak self] (success, error) in

        guard success else {
            dispatch_async(dispatch_get_main_queue()) {
                // show something here to block the user from continuing
            }

            return
        }

        dispatch_async(dispatch_get_main_queue()) {
            // do something here to continue loading your app, e.g. call a delegate method
        }
    }
}

Now, when the user taps the home button with the wrong finger, the alert box will show an "Enter Passcode" option:

enter image description here

Tapping on Enter Passcode will show one of two prompts depending on whether the user has a numeric or alphanumeric passcode set:

Numeric passcode:

numeric passcode

Alphanumeric passcode:

alphanumeric passcode