Touch ID Implementation in iOS Application

Posted By : Prince Verma | 09-Sep-2018

Apple introduced Touch ID in iPhone 5s and above versions. Touch ID basically a layer of security you can use to unlock your phone and Apple has provided this feature to use in your Application. This makes your application secure and smart. Instead of using a password you can directly perform actions by simply giving your registered fingerprint.

 

This feature can be implemented in iOS 8.0 + version. Apple didn't provide this in lower versions of iPhone.

To implement this, we have to understand the Local Authentication Framework.

 

Local Authentication:  Local Authentication is a framework given by Apple to implement Touch Id in your application. To maximise security,  Apple has built a hardware-based security processor isolated from the rest of the system which is named as "SECURITY ENCLAVE". It manages the fingerprint data out of reach even of Operating system.

It checks for authentication and returns boolean.

 

Let's assume that you have to login with Touch Id.

Make an IBAction of Login button.

Inside IBAction, make an object of LAContext.

        let authContext = LAContext()
        let authReason = "Please use touch ID to sign in."
        var authError:NSError?
       // Return true if biometric will get succeeded 
       if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &authError){
            authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: authReason) { (success, error) in
                if success{
                    print("authenticate successfully")
                    // perform logic to go ahead 
                    DispatchQueue.main.async(execute: {
                        // Login Successful
                        // main thread to perform something
                    })
                }
                else{
                    DispatchQueue.main.async(execute: {
                        self.reportTouchIDError(error: error as! NSError)
                    })
                }
            }
=        }else{
            // We have an error
            print(authError?.localizedDescription)
            //Show login screen again
        }

       // Touch Id error scenarios
       func reportTouchIDError(error:NSError){
        switch error.code{
        case LAError.authenticationFailed.rawValue:
            print("Authentication failed")
        case LAError.passcodeNotSet.rawValue:
            print("Authentication failed")
        case LAError.systemCancel.rawValue:
            print("Authentication failed")
        case LAError.userCancel.rawValue:
            print("Authentication failed")
        case LAError.userFallback.rawValue:
            print("Authentication failed")
        default:
            print(error.localizedDescription)
        }
    }
        

OUTPUT:

 

About Author

Author Image
Prince Verma

Prince Verma is working as an iOS Developer. He is very dedicated and hardworking towards his work. His interest includes Research and Development.

Request for Proposal

Name is required

Comment is required

Sending message..