How to unlock the app using Touch ID and Face ID in Swift.
Easiest way to implement Touch ID and Face ID for your app.
Touch ID and Face ID let users to access the app, in a much easier way. To access the Touch ID and Face ID, we need to import LocalAuthentication framework.
Let’s see the implementation of Touch Id and Face ID :-
- Import the LocalAuthentication framework and create an instance of LAContext.
LAContext :- A mechanism for evaluating authentication policies and access controls.
2. Check whether the device is capable of supporting biomertic authentication and is confirgured by users in the app settings by calling the method canEvaluatePolicy(_:error:).
The policy controls how the authentication behaves. For example, the LAPolicy.deviceOwnerAuthentication
policy used in this sample indicates that reverting to a passcode is allowed when biometrics fails or is unavailable. Alternatively, you can indicate the LAPolicy.deviceOwnerAuthenticationWithBiometrics
policy, which doesn’t allow reverting to the device passcode.
3. If the device is capable , then the system biometric check will begin — explaining the reason why the system is asking for authentication.
For Touch ID, the reason is written in code and for Face ID its mentioned in info.plist.
4. After the successful authentication, unlock the app.
Flow Chart for better understanding.
Now let see the code:-
HandlePressMe() will handle the code for biometric authentication. This function will be called on the click of Press Me button.
Demo
Here is the sample code in Github repository.
Reference link :-
Apple Doc :- Logging a User into Your App with Face ID or Touch ID
Touch to activate: Touch ID, Face ID and LocalAuthentication
Thank you for reading, please share your experience for LocalAuthentication in the comment box. ✌️🏻✌️🏻
Keep sharing knowledge ✍🏻