Touch ID iOS Tutorial

Touch ID refers to the fingerprint sensor built into the home button of iPhone TouchID is based on the Local Authentication framework. In this tutorial touch ID authentication will be implemented and the user is presented with an Alert View if the authentication does/doesn’t succeed. This tutorial is made with Xcode 9 and built for iOS 11.

Open Xcode and create a new Single View App.

For product name, use IOS11TouchIDTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and drag a Button to the main view. Set the title of the button to Authenticate. Select the Button and select the Auto Layout align button. Select the "Horizontally in Container" checkbox and click "Add 1 Constraint".

Select the Button and select the Auto Layout pin button. Pin the Button to the top and and click "Add 1 Constraint".

The Storyboard should look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Button to the ViewController class and create the following Action

As the Touch ID is based on the Local Authentication framework it needs to be imported to the project. Go to the ViewController.swift file and add the following line

import LocalAuthentication

Next add a helper method to display messages in a Alert Controller.

func showAlertController(_ message: String) {
    let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    present(alertController, animated: true, completion: nil)
}

A message and a OK button is added to the Alert Controller and it will be presented. Next implement the authWithTouchID method

@IBAction func authWithTouchID(_ sender: Any) {
    // 1
    let context = LAContext()
    var error: NSError?
        
    // 2
    // check if Touch ID is available
    if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
        // 3
        let reason = "Authenticate with Touch ID"
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply:
                {(succes, error) in
                    // 4
                    if succes {
                        self.showAlertController("Touch ID Authentication Succeeded")
                    }
                    else {
                        self.showAlertController("Touch ID Authentication Failed")
                    }
                } as! (Bool, Error?) -> Void)
        }
            // 5
        else {
            showAlertController("Touch ID not available")
        }
    }
  1. Get the authentication context from the Local Authentication framework
  2. The canEvaluatePolicy method checks if Touch ID is available on the device
  3. The policy is evaluated where the third parameter is a completion handler block.
  4. An Alert message is shown wether the Touch ID authentication succeeded or not
  5. If Touch ID is not available an Alert message is shown.

Build and Run the project, this must be done on a real device, since the Simulator doesn't have Touch ID functionality. 

Activate the Touch ID. When it succeeds the following alert will show.

You can download the source code of the IOS11TouchIDTutorial at the ioscreator repository on Github.