Modern App Login with Biometric Authentication
Hello there! What is the app you are working on? It might require Username/ Password Login right? Consider telling your project manager to use FaceID/TouchID to help the user scan their BioMetric to log into your app? Let’s read this post and find out what are the efforts for you to do so …
History of Biometric Authentication
BioMetric Authentication is the fancy term of TouchID/FaceID in iOS application development. The main idea is surrounding the fact that we can use TouchID/FaceID to verify the user identity then App can do the appropriate action like query data from UserDefault/KeyChain then log them into the app.
The Effort of implementing BioMetric Auth:
It is a fairly simple implementation, briefly, you need to follow 3 important steps:
- add ‘Privacy — Face ID Usage Description’ into your project info.plist
- have a manager class to handle Biometric Authentication
- have functions to store/delete user sensitive data to log them into the app
Enough talk, Let’s get started:
1.Modified Info.plist
in your info.plist add the appropriate privacy request description, in my case i simply say ‘would like to use bio metric authencation to sign you into the app.’
2. Creating a manager class to handle BioMetric Authentication
First thing first, let’s import LAContext to class. LAContext is a mechanism for evaluating authentication policies and access controls.
import LocalAuthenticationclass BioMetricManager {
private let context = LAContext()
}
Then let’s check if the device supports the BioMetric feature, and what type of BioMetric feature will be used.
Nowadays devices that are later than iPhone X support FaceID, devices from iPhone 5s — iPhone 8 plus only support TouchID, and the older devices don’t support this feature at all.
func isBioMetricFeatureAvailable() -> Bool {return context.canEvaluatePolicy(evaluationPolcy, error: nil)}var bioMetricType: LABiometryType {context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)return context.biometryType}
Now, let asked the user to allow our app to use FaceID/Touch ID, by using ‘context.evaluatePolicy’ function of the context object.
...
context.evaluatePolicy(evaluationPolcy, localizedReason: authenticationReason, reply: { (success, error) in }
...
here is the full definition of BioMetricManager class:
3. Now it is time to take action with the newly created BioMetricManager class.
assumed that your app has 3 buttons Enable Face ID, Login, and Turn off BioMetric.
- Enable Face ID Button: request user permission, start scanning face, then save user token to log in later
- Login Button: scan face to see check user identity, get sensitive data from local storage, then log the user into the app
- Turn Off BioMetric: clear the user sensitive data from the app
here are the full source code of the mentioned implementation:
Yay, this app now supports FaceID/TouchID.
check out the full source code here.
Let me know if you have any suggestion in the comments below …. ✌🏽