Touch ID in Swift

Login to Access Code

What will we be building?

We are going to build a simple application that shows a UILabel that updates based on whether or not the user successfully authentications via Touch ID.

What is Touch ID?

Touch ID is a form of authentication in which a user uses his or her fingerprint to successfully authenticate.

A cool thing to mention about Touch ID is that it is capable of reading a finger in any orientation that the phone may be in.

How to make use of Touch ID?

To use Touch ID, we need to make use of the Local Authentication framework.

The Local Authentication Framework provides us with the facilities for requesting authentication from users with specific security policies. In our case, it allows us to request for fingerprint authentication.

To make use of Touch ID in our application, we need to do the following:

  1. Create a Local Authentication Context object.

    1. Authentication contexts are used to evaluate authentication policies, allowing apps to request the user to authenticate themselves using personal information such as a fingerprint registered with Touch ID.
  2. Create a variable to hold errors.

  3. Create a variable which explains why our application needs authentication.

  4. Request Authentication

  5. Handle Success and Errors. In our case we will update our UILabel.

Let us get started

To get started, download the starter repository at the top of this tutorial by clicking on Source Code. This will bring you to the github page.

Let’s create a method that encapsulates our Authentication request.

Next we need to create our Local Authentication Context object and the variable that we will pass to it.

Now we need to check that a user is capable of Authenticating with Touch ID. We will call .canEvaluatePolicy on our context, passing in the policy we want to Authenticate with and our error holding variable.

Now that we’ve verified if the user can Authenticate via Touch ID, we need to actually request the authentication. We will do that with the .evaluatePolicy method on the context object. Which takes our Policy, the reason for authentication, and a block.

This method evaluates asynchronously. The reply block is where we will handle the evaluation. The evaluation happens on a private queue internal to the framework in an unspecified threading context. So any UI updates must be done in the main thread. So we will wrap our UI updates in a dispatch_async block, indicating that we want to update it on the main thread.

Because we are awesome programmers and awesome programmers DRY their code, let’s move the label updating to it’s own method.

Now we have DRYer code.

The final step is to now add a call to this method in viewDidLoad. When your application starts up it will ask you to Authenticate via Touch ID.

That’s it for this tutorial, go ahead and run your application to ensure that it’s still working.

Feel free to leave any comments and let us know how we can better improve our tutorials for you.