HowTo: Use Face ID and Touch ID in your app

Ishwar Janwa
Mindful Engineering
3 min readNov 9, 2020
Photo by Bagus Hernawan on Unsplash

FaceID and TouchID can be used in apps to authenticate device Owner. Users love Touch ID and Face ID because they allow them to effortlessly authenticate, Let’s see how we can use the same well known ways into our apps.

How to add FaceID/TouchID

Add Face ID Usage Description in info.plist

In any project that uses biometrics, include the NSFaceIDUsageDescription key in your app’s Info.plist file. Without this key, the system won’t allow your app to use Face ID.

Create and Configure a Context

You perform biometric authentication in your app using an LAContext instance, which interaction between your app and the Secure Enclave.

var context = LAContext()

You can customize the messaging used by the context to guide the user through the flow

context.localizedCancelTitle = "Enter Username/Password"

This helps the user understand that when they tap the button, they’ll be reverting to your normal authentication procedure.

Check FaceID and TouchID Policy Availability

Before attempting to authenticate, test to make sure that you actually have the ability to do

var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {
}

Authenticate FaceID and TouchID

When you’re ready to authenticate, call the evaluatePolicy(_:localizedReason:reply: method, using the same policy you already check availability:

let reason = "Secure your account details"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { success, error in

if success {

DispatchQueue.main.async { [unowned self] in
print("Successfully authenticate")
}

} else {
print(error?.localizedDescription ?? "Failed to authenticate")
}
}

For Touch ID, or when the user enters a passcode, the system displays the reason for authenticating that you provided in the method call. It’s important to provide a clear explanation of why your app is asking the user to authenticate. The name of your app already appears before the reason you give, so you don’t need to include that in your message.

Fallback Alternative

For various reasons, authentication fails or is unavailable:

  1. The user’s device doesn’t have Touch ID or Face ID.
  2. The user isn’t enrolled in biometrics, or doesn’t have a passcode set.
  3. The user cancels the operation.
  4. Touch ID or Face ID fails to recognize the user.

if you encounter a authentication error, fall back to your own authentication scheme, like asking for a username and password. Use FaceID/TouchID as a supplement to something you’re already doing. Don’t depend on biometrics as your only authentication option.

For reference, you can download the project form Here

Try testing in a real device. If you don’t have a real device no worries, use the Simulator’s Hardware->Face ID /Touch ID options.

Disclaimer: To make this article I had to read a lot of stuffs over the internet. Also, I had copied easy to understand examples and sentences from other article to make this article meaningful.

--

--