iOS Touch ID Authentication Tutorial

Neha Sharma
3 min readJan 9, 2018

Apple introduce TouchID in iPhone 5s, in which instead of entering password, user have to give their fingerprint to unlock the device of any functionality in iPhone.

In OS 7, Apple didn’t published the FingerPrint API’s to developer but in OS 8, Apple provide us the API to use this functionality in your iOS App.

In this tutorial I will explain how you can use this functionality in your iOS Application.

  1. Local Authentication Framework

To add TouchID Functionality, you have to add LocalAuthentication Framework in to your project.

Go to Project Settings > General and scroll down to the Linked Frameworks and Libraries section. Click on the + sign and add LocalAuthentication.framework.

Add Local Authentication Framework

2. Authentication View

For TouchID, we will create one login button and onClick of button we will provide TouchID Authentication screen for user to unlock the feature.

@IBAction func clickLoginButton(_ sender: Any){  self.authenticateUSerTouchID()}

3. For Success & Failure of TouchID Authentication

func authenticateUserTouchID() {let context : LAContext = LAContext()// Declare a NSError variable.let myLocalizedReasonString = "Authentication is needed to access your Home ViewController."var authError: NSError?if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: myLocalizedReasonString) { success, evaluateError inif success // IF TOUCH ID AUTHENTICATION IS SUCCESSFUL, NAVIGATE TO NEXT VIEW CONTROLLER
{
DispatchQueue.main.async{print("Authentication success by the system")let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)let homeVC = storyBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewControllerself.navigationController?.pushViewController(homeVC, animated: true)}}else // IF TOUCH ID AUTHENTICATION IS FAILED, PRINT ERROR MSG
{
if let error = error {let message = self.showErrorMessageForLAErrorCode(error.code)print(message)
}
}}}}

4. Print error message on Touch ID Authentication Failure

func showErrorMessageForLAErrorCode( errorCode:Int ) -> String{

var message = ""

switch errorCode {

case LAError.AppCancel.rawValue:
message = "Authentication was cancelled by application"

case LAError.AuthenticationFailed.rawValue:
message = "The user failed to provide valid credentials"

case LAError.InvalidContext.rawValue:
message = "The context is invalid"

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.TouchIDLockout.rawValue:
message = "Too many failed attempts."

case LAError.TouchIDNotAvailable.rawValue:
message = "TouchID is not available on the device"

case LAError.UserCancel.rawValue:
message = "The user did cancel"

case LAError.UserFallback.rawValue:
message = "The user chose to use the fallback"

default:
message = "Did not find error code on LAError object"

}

return message

}

Try TouchID Authentication Integration in your application.

What do you think about this tutorial? Love to hear your feedback.

Please share and give some claps so others can also find it useful 👏👏👏👏👏👏 !!!!

Follow Me on: Facebook | Twitter | GitHub | LinkedIn

--

--

Neha Sharma

Sr. iOS Developer & Free Time Blogger. I am both driven and self-motivated and constantly experimenting with new technologies and techniques.