iOS Tutorial – The Easiest Way to Integrate Touch ID Authentication in Your iOS App

0
Shares
iOS Tutorial – The Easiest Way to Integrate Touch ID Authentication in Your iOS App
Rate this post

A few years back, Apple had introduced all-new Touch ID on the iPhone 5s and since then, it’s in the market. This Apple Touch ID allows a user to unlock their device with their fingerprint, instead of asking password. This Touch ID has improved the UX by gazillion times.

However, in the iOS 7, it was impossible for developers to use Apple Touch ID for authentication in iOS apps. But after the launch of iOS 8, Apple has officially provided this API to use in iOS apps.

In this iOS tutorial, we’re going to share the entire process of how to integrate touch id authentication in your iOS app from the scratch.

Let’s Begin

Create a new project under the file menu and select “Single View Application” then click on next button.

Integrate Touch ID Authentication in Your iOS App

TouchID2

 

In the next tab, enter the product name and click on next. Here, we’re naming demo as an “SOTouchAuthentication”.

TouchID3

Once you create a new project, import Local Authentication in “ViewController.swift”.

Next, create an object of “LAContext” in ViewDidLoad() method.

//Create a context
let authenticationContext = LAContext()

Now check the device compatibility. If the device has BiometricSensor, then we can implement Fingerprint detection.

var error:NSError?

//Check if device have Biometric sensor
let isValidSensor : Bool = authenticationContext.canEvaluatePolicy(.DeviceOwnerAuthentication, error: &error)

if isValidSensor {
//Device have BiometricSensor
//It Supports TouchID
} else {
//Device not support TouchID
//For reason get error code from here
let strMessage = self.errorMessage(error!.code)
self.showAlertWithTitle("Error", message: strMessage)
}

After knowing that device supports Touch ID, it’s time to go for authentication.

authenticationContext.evaluatePolicy(
.DeviceOwnerAuthenticationWithBiometrics,
localizedReason: "Authenticate",
reply: { [unowned self] (success, error) -> Void in

if( success ) {
//Fingerprint recognized
self.goToNextVC()

} else {
//If not recognized then
if let error = error {
let strMessage = self.errorMessage(error.code)
self.showAlertWithTitle("Error", message: strMessage)

}

}
})

The following are the different type of error messages we might get during Touch ID authentication according to an error code.

func errorMessage(errorCode:Int) -> String{

var strMessage = ""

switch errorCode {
case LAError.AuthenticationFailed.rawValue:
strMessage = "Authentication Failed"

case LAError.UserCancel.rawValue:
strMessage = "User Cancel"

case LAError.UserFallback.rawValue:
strMessage = "User Fallback"

case LAError.SystemCancel.rawValue:
strMessage = "System Cancel"

case LAError.PasscodeNotSet.rawValue:
strMessage = "Passcode Not Set"

case LAError.TouchIDNotAvailable.rawValue:
strMessage = "TouchI DNot Available"

case LAError.TouchIDNotEnrolled.rawValue:
strMessage = "TouchID Not Enrolled"

case LAError.TouchIDLockout.rawValue:
strMessage = "TouchID Lockout"

case LAError.AppCancel.rawValue:
strMessage = "App Cancel"

case LAError.InvalidContext.rawValue:
strMessage = "Invalid Context"

default:
strMessage = "Some error found"

}

return strMessage

}

Lastly, to show an alert message.

func showAlertWithTitle( title:String, message:String ) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)

let actionOk = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(actionOk)

self.presentViewController(alert, animated: true, completion: nil)
}

After the completion of Touch ID authentication, user can perform next action in your iOS app. For example:

func goToNextVC() {
let objHomeVC = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVCID")
self.presentViewController(objHomeVC!, animated: true, completion: nil)

}

By following these steps, you can successfully implement the Touch ID authentication. And as you know, Touch ID authentication is a cool feature with the simple implementation.

If you face any problem implementing this feature in iOS app, you can contact our developers. This feature must be included in every sensitive part of your app.

Also, iPhone users love the Touch ID and will be happy to use it. So, if you’ve any app idea and want to implement Touch ID authentication, you can contact us for the same. We’re an iPhone app development company in India with 5+ years of experience and we’ve developed more than 2000 mobile apps throughout our journey.

You can get a copy of this demo source code to integrate touch id authentication in your iOS app.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!

One thought on “iOS Tutorial – The Easiest Way to Integrate Touch ID Authentication in Your iOS App

  1. dam ponting

    Protecting an app with a login screen is a great way to secure user data
    – you can use the Keychain, which is built right in to iOS, to ensure
    that their data stays secure. However, Apple now offers yet another
    layer of protection with Touch ID, available on the iPhone 5s, 6, 6+,
    6S, 6S+ iPad Air 2, iPad Pro, iPad Mini 4 and iPad Mini 3.

    Reply

Leave a Reply

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