Integrating Face ID/Touch ID in iOS
Did you know Facial Recognition dates back to 1960s with manual measurements of facial coordinates. It’s used by govt agencies like DOJ, FBI, DEA, CBP and U.S. Marshalls. In-fact US-Govt used facial recognition to confirm the identity of Osama bin Laden for Operation Neptune Spear in 2011.
But with the introduction of Face-ID in iPhone X, Apple changed history. Now, iPhone X can analyse 30,000 facial dots in just 1 S (30% improvement in iPhone 11) with 1 error in 1 M (obviously excluding Twins 😍 😍). And now it’s coming with Masks on in iOS 13.5, thanks to the pandemic 😢 😢.
Maker ➡️ Fan-Boy ➡️ Reader :
Enough of Past & Present, Let’s dive into Future :
Let’s implement. When it comes to the authentication mechanisms, Apple’s baby is LocalAuthentication framework. It has 3 main components:
LAContext : An authentication context object is used to evaluate the user’s identity, either with biometrics like Touch ID or Face ID, or by supplying the device passcode.
canEvaluatePolicy : An API called on context to determine if a particular policy can be evaluated. Returns true if the policy can be evaluated, otherwise false.
- Policy basically decides if you need just Face ID/Touch ID or both Face ID/Touch ID & Device Passcode. In our case we will use prior, deviceOwnerAuthenticationWithBiometrics (recommended, by Apple)
- It also takes a NSError Pointer to return back the LAError Code in case it can’t evaluate the policy.
- After getting a Success/Failure, we can know about LABiometryType, if the current device supports Face-ID/Touch-ID or none.
evaluatePolicy : An API called on context after a success from canEvaluatePolicy. It begins authenticating Face-ID with animating HUD.
- It’s asynchronous
- It takes a policy to evaluate, and a localizedReason (will see later in here) for it.
- Returns a closer with Success/Failure & an Error if any with LAError Code.
Now, Let’s go with the Flow :
Wrapper makes it Better :
- Let’s create a wrapper file named BiometricIDAuth.swift.
- Import LocalAuthentication
- Advantage of having a wrapper is that, we don’t need to keep importing the dependencies everywhere else in the App, where it’s used by the clients.
- Let’s add the LAContext & LAPolicy properties discussed earlier.
- Now, we will add few properties like localizedReason, localizedFallbackTitle & localizedCancelTitle needed for customising the context.
- We will also keep a NSError? property that will be sent to canEvaluatePolicy as a pointer as discussed earlier.
Our BiometricIDAuth.swift will now look something like this:
- Next, let’s add two enums:
BiometricType, will map LABiometryType of our LAContext
BiometricError, will map NSError returned from canEvaluatePolicy & evaluatePolicy. It confirms to LocalizedError that will help us with displaying proper error messages to User.
- Next, let’s add the methods that will actually do this mapping
- Next, let’s add the two main functions which we have been waiting for, which will do the magic for our Face-ID/Touch ID authentication.
- Starting with canEvaluatePolicy :
2. And then, evaluatePolicy :
Think globally, Act locally :
Now, let’s understand what all those localizedReason, localizedFallbackTitle & localizedCancelTitle needed for customising the context are, by recalling the failure popups from the earlier Flow Chart:
Join the Dots :
Let’s bring the wrapper into use.
- Create AuthenticationViewController.swift
- Add a property for BiometricIDAuth
private let biometricIDAuth = BiometricIDAuth()
- Create an IBAction for “verify with Face ID” button and put the below code in :
Last, but not the least :
For Face ID, We have to include the NSFaceIDUsageDescription (Privacy Face ID Usage Description) key in our app’s Info.plist
file. Without this key, the system won’t allow our app to use Face ID.
If your app needs to use just Touch ID, you don’t need to do above step. (Just Kidding, there will be no such case 😂😂)
Demo Time 🚀🚀:
What if I don’t have a Device 😎
Well guess what, we’re Done :
I feel Face-ID/Touch-ID is a seamless way to onboard users and access sensitive information leveraging Apple’s Secure Enclave. It’s a great feature to have on Modern Apps.
Checkout the complete code at Github Repo.
Find me on Github & Linkedin. If you liked the article, hit the 👏 button. If you didn’t, please put your suggestion for improvement on comments. Do check the great references below for more information.