Integrate the FaceID or TouchID Authentication in iOS Apps

Updated 28 September 2018

Save

Dear Folks, today I am going to explain the proper way to integrate the Face ID or Touch ID in iOS App. Face ID and Touch ID are secure, familiar authentication methods that people trust. Follow below steps:

Step 1: Add NSFaceIDUsageDescription Key in info.plist file

Step 2: Create a swift file with any name but it would be better to related to its functionality, Like BioMetrixFile.swift

Step 3: In BioMetrixFile write below codes and follow instructions. But before proceeding further import LocalAuthentication class in BioMetrixFile.

Now, create typealias for success or failure blocks to well manage Authentication Success or Failure.

Now written all string messages success or error, created enum for them.

Step 4: Created a Singleton class with name BioMetrixAuthentication which is subclass of NSObject.

Step 5: Added a class function to check whether device has TouchID available or not.

Step 6: Added a function to Check BioMetrix Authentication (FaceId / Touch Id)

Step 7: Add a function to check device passcode if user fails to authenticate himself using touchID or faceID several times.

Step 8: Add a method to check if device supports faceID or not.

Step 9: Add a method to show authentication message regarding faceID or TouchId. If device supports faceID, it will pick the value from BioMetrixFaceIDError Enum otherwise from BioMetrixTouchIDErrors Enum.

Step 10. Now Add a method to handle the error message, when user has too many failed attempts for faceID or TouchId.

Step 11. Now Add method to evaluate the specified policy for faceID or TouchID. More info: https://developer.apple.com/documentation/localauthentication/lacontext

Step 12: Add AuthenticationError enum to manage errors.

If you followed all the steps mentioned above, your BioMetrixFile.swift will look like this.

Now moving to the UIViewController, where you want to integrate the FaceID or TouchID. Create UIButton IBAction method and follow below proper steps to manage the correct flow of authentication. Adding below lines of code in IBAction method of this button.

Steps 13: Starting Authentication by calling the class function of BioMetrixAuthentication Class in BioMetrixFile.swift file.

Step 14: Add method to show passcode authentication

Step 15: Adding a common method for login success.

Step 16: Handling the situation, when user has not enabled FaceId or TouchId.

I would love to hear from you, if you have any doubt regarding code please leave comment below.

 

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home