iOS Biometric Authentication Implementation in Swift

How to easily implement biometric authentication in your app

Domenico Nicoli
Oct 23 · 3 min read
Photo by Lukenn Sabellano on Unsplash


In 2017 Apple launched its new top tier smartphone model: iPhone X.

Unlike previous models, it has a full-frame screen without a bezel (except for a notch). Due to this radical change, the home button also disappears and with it fingerprint security authentication.

But Apple is not crazy. They introduced a sophisticated new method to secure access to our smartphone: Face ID.

Face ID is a facial recognition system that works thanks to three different hardware sensors: a dot projector, a flood illuminator, and an infrared camera. The resultant map is compared with the pregistered face — if the two faces match, the user is authenticated. The system works well, even with glasses, hat, facial hair, and make up.

Getting Started

First, open Xcode and create a new project. Select Single View App.

Now go to Project Settings > General, scroll down to the Linked Frameworks and Libraries section. Click on + sign and add LocalAuthentication.framework.


We will start with the creation of a new method called authenticationWithTouchID. Here we put the logic to authenticate the user using biometrics (fingerprints/face recognition) or with passcode as a fallback.

To do this we use two methods:

  • canEvaluatePolicy method — returns false if device owner authentication is not available.
  • evaluatePolicy method — requests authentication from the user through biometrics or passcode.

Basically, our app will show the facial recognition popup first (or fingerprints popup, if our device does not support Face ID) and then the fallback button after the first unsuccessful attempt to use the passcode.

func authenticationWithTouchID() {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Please use your Passcode"

var authorizationError: NSError?
let reason = "Authentication required to access the secure data"

if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authorizationError) {

localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, evaluateError in

if success {
DispatchQueue.main.async() {
let alert = UIAlertController(title: "Success", message: "Authenticated succesfully!", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)

} else {
// Failed to authenticate
guard let error = evaluateError else {

} else {

guard let error = authorizationError else {

You can find the gist URL here.

Time to Test It

We’re already finished! It’s time to test it on our physical device.

Face ID popup prompt on left | Successfully authentication on right
Face not recognized on left | Passcode as fallback on right

You can find the full code HERE

Thank you for reading. If you have any questions or suggestions, please let me know in the comments.

Better Programming

Advice for programmers.

Domenico Nicoli

Written by

Backend Developer (C#, VB.NET, Microsoft SQL Server), iOS developer (Swift). Parma, Italy 🇮🇹

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade