Touch ID and Face Id Authentication in Swift 4

Posted By : Kishan Gupta | 25-Nov-2018


1. Introduction

2. Prerequisites

3. Let's start
4. Output

5. Conclusion

6. Reference




With the introduction of iPhone 5s, Apple introduces a revolutionary way to unlock the iPhone.
Touch ID - It brings revolution in the mobile phone industry. It's new, Fastest ever, and super easy and comfortable way to unlock your iPhone. And with the launch of iPhone X, it changes the whole of authentication by introducing Face ID

In iOS 8, Apple let the developers use Touch ID to unlock your application. This allows the developers to add more security to their application.

In this blog, we will see how to use Touch ID in your application.



1. Hardware
->IPhone 5s and above for Touch ID
->Iphone 10 and above for Face ID

2. Software
-> iOS 8 and above to support Touch ID
-> iOS 10 and above to support Face ID 


Let's start:-


Now we are ready to add Toucha Id/Face ID in our application.
To use Touch ID and Face ID in our application first we need to add Local Authentication Framework in our application.
To add this framework:-
1. Go to project setting.
2. Select general from head buttons
3. Tap on the + button on Linked Framework and Libraries section and add LocalAuthentication Framework in your project.



Now open your required view controller and import LocalAuthentication.
first, we need to create to create an object of LAContext class of LocalAuthentication framework


let localAuthenticationContext = LAContext()


let localAuthenticationContext = LAContext()
now we need to select a LAPolicy to evaluate
we are using deviceOwnerAuthentication: It authenticates the device owner using biometric or the device password.

now, we have to check whether the iPhone biometrics is enabled or not.


if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
     //Write the your code here


this returns false if  Biometric is not enabled in device else it proceeds with touch id and face id authentication.

Following is the complete view controller with local authentication implemented in it.


import UIKit
import LocalAuthentication

class ViewController: UIViewController {

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.

extension ViewController {
    func authenticationWithTouchID() {
        let localAuthenticationContext = LAContext()
        localAuthenticationContext.localizedFallbackTitle = "Use Passcode"

        var authError: NSError?
        let reasonString = "To access the secure data"

        if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
            localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString) { success, evaluateError in
                if success {
                    //TODO: User authenticated successfully, take appropriate action
                } else {
                    //TODO: User did not authenticate successfully, look at error and take appropriate action
                    guard let error = evaluateError else {
                    print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
                    //TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
        } else {
            guard let error = authError else {
            //TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
            print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error.code))
    func evaluatePolicyFailErrorMessageForLA(errorCode: Int) -> String {
        var message = ""
        if #available(iOS 11.0, macOS 10.13, *) {
            switch errorCode {
                case LAError.biometryNotAvailable.rawValue:
                    message = "Authentication could not start because the device does not support biometric authentication."
                case LAError.biometryLockout.rawValue:
                    message = "Authentication could not continue because the user has been locked out of biometric authentication, due to failing authentication too many times."
                case LAError.biometryNotEnrolled.rawValue:
                    message = "Authentication could not start because the user has not enrolled in biometric authentication."
                    message = "Did not find error code on LAError object"
        } else {
            switch errorCode {
                case LAError.touchIDLockout.rawValue:
                    message = "Too many failed attempts."
                case LAError.touchIDNotAvailable.rawValue:
                    message = "TouchID is not available on the device"
                case LAError.touchIDNotEnrolled.rawValue:
                    message = "TouchID is not enrolled on the device"
                    message = "Did not find error code on LAError object"
        return message;
    func evaluateAuthenticationPolicyMessageForLA(errorCode: Int) -> String {
        var message = ""
        switch errorCode {
        case LAError.authenticationFailed.rawValue:
            message = "The user failed to provide valid credentials"
        case LAError.appCancel.rawValue:
            message = "Authentication was cancelled by application"
        case LAError.invalidContext.rawValue:
            message = "The context is invalid"
        case LAError.notInteractive.rawValue:
            message = "Not interactive"
        case LAError.passcodeNotSet.rawValue:
            message = "Passcode is not set on the device"
        case LAError.systemCancel.rawValue:
            message = "Authentication was cancelled by the system"
        case LAError.userCancel.rawValue:
            message = "The user did cancel"
        case LAError.userFallback.rawValue:
            message = "The user chose to use the fallback"

            message = evaluatePolicyFailErrorMessageForLA(errorCode: errorCode)
        return message


Output : -



on selecting passcode option following screen will pop up




LocalAuthentication has allows the Developers to more security in their application as security is now one of the biggest concern of the user.
After implementation of Touch Id and Face Id complete outlook looks like the following.


Do share if you like the blog.

About Author

Author Image
Kishan Gupta

Young, Enthusiastic, Never give up Attitude are the perfect words to define Kishan. He is committed to learn and dedicated to work. He believes in "Think Big Get Big"

Request for Proposal

Name is required

Comment is required

Sending message..