FaceId authentication in iOS11, Swift 4

iPhone X released on 11/03/2017 and the most intriguing feature is FaceId authentication. How about adding FaceId authentication for login to our own app?. This article covers code implementation for it. Let’s dive in!

Complete movie demo of the code is uploaded here. All the code in this article can be downloaded / cloned from github.
Here is the screenshot of the output of the project explained in this article.


Apple provides the LocalAuthentication framework from iOS 8 for touch Id authentication. Face Id uses the same framework.

The LocalAuthentication framework provides facilities for requesting authentication from users with specified security policies. LocalAuthentication automatically provides the interface for evaluating authentication policies and access controls, managing credentials, and invalidating authentication contexts with LAContext

Evaluating Authentication Policies:

LAContext object provides the below methods to check device capability for biometric authentication and to do the actual authentication.

    1. canEvaluatePolicy : This method returns true if device is ready for biometric authentication. Otherwise, it returns false and populates the NSError with the error code.

    2. evaluatePolicy:  If canEvaluatePolicy returns true, we can call evaluatePolicy that shows interface for biometric authentication along with multiple prompt messages for retry option. The reply completion handler returns arguments (success,error) which can be used to determine success or the failure error code.

User Authentication Prompts:

LAContext provides below 3 string properties to prompt the user

var localizedReason: String -  The localized explanation for authentication shown in the dialog presented to the user.

var localizedFallbackTitle: String? -  The localized title for the fallback button in the dialog presented to the user during authentication.

var localizedCancelTitle: String? - The localized title for the fallback button in the dialog presented to the user during authentication.

Here is what see so far.


Authentication Fail Error Reason

When authentication fails either at canEvaluatePolicy or at evaluatePolicy, error code is that is returned can be used to fail reason.

Below is the complete function that maps the error reason for each error code.

The GitHub link for this project has MTBiometricAuthentication class that you can add to your project. Just create an instance of it,  add notification observer as below.

Now call authenticationWithBiometricID() function and app will notified of success or fail reason via post notification.  You are all set with biometric authentication.  After evaluating authentication, app will be notified with success or fail reason in post notification handler as in below code.

For any questions  on this topic, drop a comment and I will be glad to respond. Happy coding!

Passionate about learning new things. Loves coding and problem solving. Built apps from scratch on iOS platform with Swift, Objective – C,  HTML 5 / javascript, Cardova, Xamarin using both MVC and MVVM. Coded extensively in .Net and Database technologies before moving to mobile development.

Spends free time playing with my kid and watching TV.