Touch ID Tutorial in iOS8 with Swift

In iOS 8 developers can add TouchID  to their applications.The  TouchID is based on Local Authentication framework. In this tutorial we will add the Touch ID authentication and the user is presented with an Alert View if the authentication does/doesn’t succeed. This tutorial is built with iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftTouchIDTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

o to the storyboard and drag a Button to the View Controller. Set the title of the button to Authenticate. Change the background color of the View to light-gray. Select the button and and select the "Resolve Auto Layout Issues" button at the bottom-right(3rd button) and select Add Missing Constraints.

The storyboard should look like this.

elect 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))
    presentViewController(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 authenticateWithTouchID method

@IBAction func authenticateWithTouchID(sender: AnyObject) {
    // 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: Bool, error: NSError!) in
          // 4
          if succes {
            self.showAlertController("Touch ID Authentication Succeeded")
          else {
            self.showAlertController("Touch ID Authentication Failed")
    // 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 using an where the third parameter is a completion handler blocks
  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 does;t have Touch ID functionality.

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