How to Implement FaceID or TouchID in iOS | by Edge Suku | October, 2022

With UIKit and SwiftUI implementations

image from apple

Biometric authentication is a new form of secure move that reduces the complex algorithms and process of operations. Let’s find out how you can use FaceID/TouchID on your app.

Before you start coding, you need to know some basics of biometric authentication. Swift handles biometric authentication LocalAuthentication Structure and use LAContext as class. Apple limits attempts at FaceID to three and TouchID to two unless it prompts the user to enter a lock screen code or navigate back to the login screen. You can customize the Cancel button localization and the “Use Password” button as fallback options.

Before applying them, we need to add our Privacy – FaceID usage permission on our Info.plist file. TouchID does not require this type of permission.

Since I’ll be showing this example on SwiftUI/UIKit, I’ve created a shared utility manager with “AuthManager. I’ll define it as a ObservableObject, I added two variables; One for checking biometric data type (none if it is unavailable), and the other to help me stop/display biometric data availability.

Now, I will add two methods named askBiometricAvailability And authenticate,

With this method, I will check if we can use any biometric authentication. If the user has not allowed our first pop-up about FaceID or has toggled off biometric data from our app menu, it will give us an error based on that. To act on it would be the closure of the function so that we can use it on our VC or View. In this function, we will check if we can access the biometric authentication a inout Variables to tell us what went wrong in the form of NSError data.

Now, we will add our authentic function. This function will have a result constructor which will take a boolean success case or LAError, which Swift offers us. Thus, we can use every case that Apple gives us as an error. You don’t have to be manipulative in every case, but it’s important to know what suit Apple can throw.

Our argument is complete. Let’s see how we implemented this in our UIKit/SwiftUI views.

I my. add two buttons to ViewController Look for this project: a FaceID/TouchID button, a button that will show me the SwiftUI view, and a label that will print the position.

in my ViewController class, my . called to askBiometricAvailability function, which will print the error case if the user has not allowed FaceID to toggle off the subject. I also checked the biometric type and changed the text/image of the button depending on the technology.

Then on button press, I added my authenticate function, which will give us error or success message depending on its status.

On SwiftUI, I added the same controller to an . called as ObservedObject In which I can control the current status of biometric type.

The app is ready to use. You can use the Feature → FaceID section of the simulator to test your app. Don’t forget to select the nominee first.

You can see the code of the app here.

Biometric authentication is easy to manage with the given APIs and tools. Apple recommends the technology depending on the device. If the user uses an iPhone SE 2 or iPad 10th generation app, don’t show the “Use FaceID” nomenclature on your button. You can also improve this code by checking for availability whenever the user returns to the app from settings or on each attendance.

Apple also recommends that you need to notify users of a FaceID/TouchID scan with a button that they may trigger because TouchID has the option to cancel without any input. It will automatically scan the face when activated by your app.

have any questions? feel free to ask.

have a nice day.

Leave a Comment