How to integrate Biometric authentication into your iPhone apps
Apple iOS always believes in implementing the latest updated technology on their devices. Last year it launched the iPhone X and this year we saw the rise of iPhone XR and iPhone XS with various new advanced features and functionalities. One of the popular functionalities is the introduction of the Touch ID and Face Id.
However, Apple has already introduced the Touch ID and it is
available via the iPhone 5S models. This is a unique feature that allows the
user to unlock his devices with their fingerprints. Therefore, technology had
to go one step further, with users not having to remember the password key or
the lock pattern.
Initially, mobile
app developers found it difficult to verify the Touch ID in the iOS7
version, but with the release of the iOS8, Apple successfully solved this
problem because it provided official API support to use the apps.
So let's start with the integration of the touch ID
authentication in the iOS app.
Create a new project
The first step is to create a new project under the file
menu and then choose the Single view application. Then click on the next
button. You would get the interface something like in the figure below.
Give your project a name
In the next step you have to give your project a name. For
example, you can give it a name as Biometrics authentication.
After successfully completing the project, it is time to
enter the local verification
One must go for the configuration of the Face ID privacy
statement in the Info. list file overview of the project.
This is the privacy statement a user sees when the app wants
permission to use Face ID authentication. For the addition of this privacy
statement, first choose the Info. list file in the navigator panel project and
click the + button at the bottom of the list item.
Then choose the Privacy - Face ID Use Description from the
desired outcome of the previous program. Now add the description to the desired
field as shown in the image.
Go through the availability of biometric authentication
But wait a minute. Have you checked the availability of
biometric authentication? It is a necessary step to follow because not all iOS
devices have the capability of a fingerprint scan and do not support the
integration of touch ID and face ID.
Therefore, you must first check the availability of
biometric authentication. This applies to both the integration of the touch and
face ID.
Check the accessibility of biometrics
It is important that you check the biometric availability
before proceeding. If your device can support the biometric sensor, you can
think of implementing the fingerprint scan or face member authentication.
The tracking code indicates whether the device supports the
Touch ID and the Face ID and updates the text on the authButton instance
accordingly.
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
// The first step would be to define few properties on the
global level for your view controller
@IBOutlet weak var imgAuthenticate: UIImageView!
let context =
LAContext()
var strAlertMessage
= String()
var error: NSError?
override func
viewDidLoad() {
super.viewDidLoad()
if context.canEvaluatePolicy(
LAPolicy.deviceOwnerAuthenticationWithBiometrics,
error:
&error) {
if
context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error:
&error) {
switch
context.biometryType {
case .faceID:
self.strAlertMessage = "Set your face to authenticate"
self.imgAuthenticate.image = UIImage(named: "face")
break
case .touchID:
self.strAlertMessage = "Set your finger to authenticate"
self.imgAuthenticate.image = UIImage(named: "touch")
break
case .none:
print("none")
//description =
"none"
break
}
}else {
// Device cannot
use biometric authentication
if let err = error
{
// calling error
message function based on the error type
let strMessage
= self.errorMessage(errorCode: err._code)
self.notifyUser("Error",
err: strMessage)
}
}
}else{
if let err = error {
let strMessage
= self.errorMessage(errorCode: err._code)
self.notifyUser("Error",
err: strMessage)
}
}
}
Getting the Biometric Authentication from User
Once you are confirmed, that it would support the Touch Id,
you can go with the following command:
@IBAction func authenticateUser(_ sender: Any) {
// Device can use biometric authentication
if
context.canEvaluatePolicy(
LAPolicy.deviceOwnerAuthenticationWithBiometrics,
error:
&error) {
context.evaluatePolicy(
.deviceOwnerAuthenticationWithBiometrics,
localizedReason: self.strAlertMessage,
reply: {
[unowned self] (success, error) -> Void in
DispatchQueue.main.async {
if(
success ) {
//Fingerprint recognized
// Do
whatever action you want to perform
} else {
//If not
recognized then
if let
error = error {
let
strMessage = self.errorMessage(errorCode: error._code)
print(strMessage)
}
}
}
})
}
}
Facing the Error Challenges During Integration
As an iOS developer, you must prepare beforehand to tackle
the challenges related to errors while implementing the Touch Id authentication
in your device. The error code may appear as:
// MARK: Get error message
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.biometryNotAvailable.rawValue:
strMessage =
"TouchI DNot Available"
case
LAError.biometryNotEnrolled.rawValue:
strMessage =
"TouchID Not Enrolled"
case
LAError.biometryLockout.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
}