prince kumar
Biometric enablement for native apps
3 min readSep 6, 2019

--

Login by TouchId swift code implementation

As the Touch ID is based on the Local Authentication framework it needs to be imported to the project. Go to the implementation controller lets say ViewController.swift file and add the following line

import LocalAuthentication

The first step that is always required when using the TouchID and the Local Authenticationframework, is to get the authentication context from the framework, exactly as shown below:

func authenticateUser() {// Get the local authentication context.let context : LAContext = LAContext()}

Next add a helper method to display messages in a Alert Controller.

func showAlertController(_ message: String) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
}

The next step is to ask the framework if the TouchID authentication can be applied to the specific device, by calling a special function named canEvaluatePolicy. It accepts two parameters, the policy we want to evaluate and an error object. Here’s how it’s used:

func authenticateUser() {// Check if the device can evaluate the policy.if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {}}

A message and a OK button is added to the Alert Controller and it will be presented. Next implement the authWithTouchID method

@IBAction func authWithTouchID(_ sender: Any) {
// 1
let context = LAContext()
var error: NSError?

// 2
// check if Touch ID is available
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// 3
let reason = "Validate the TouchId"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason, reply:
{(success, error) in
// 4
if success {
self.showAlertController("Touch ID Authentication Succeeded")
}
else {
self.showAlertController("Touch ID Authentication Failed")
}
})
}
// 5
else {
showAlertController("Touch ID not available")
}
}
  1. Get the authentication context from the Local Authentication framework
  2. The canEvaluatePolicy method checks if Touch ID is available on the device
  3. The policy is evaluated where the third parameter is a completion handler block.
  4. An Alert message is shown wether the Touch ID authentication succeeded or not
  5. If Touch ID is not available an Alert message is shown.

Build and Run the project, this must be done on a real device, since the Simulator doesn’t have Touch ID functionality.

TouchId system pop up

On successful validation , you get the success pop up, We have a custom pop up in this case.

Touch Id successful validation

This is a pretty straight code used for the integration of the TouchId in the iOS apps and the similar code is used for FaceId detection.

The local authentication framework has LABiometryType, which informs the app which kind of the support the device has like

image taken from the apple developer site

The apple developer link for the local authentication:

https://developer.apple.com/documentation/localauthentication/lacontext/2867583-biometrytype

--

--