How to add FaceID/TouchID using Swift 4

ANANTHA KRISHNAN K G
Swift Dynamics
Published in
2 min readMay 27, 2018

FaceID and TouchID can be used in apps to authenticate users for in-app purchase or accessing personal info or more. Check the wallet apps, almost all of them have TouchID enabled.

Okay ,let’s get see what Apple say about these auth mechanisms.

Ask users to authenticate only in exchange for value, such as personalizing the experience, accessing additional features, purchasing content, or synchronizing data. If your app requires authentication, keep the sign-in process quick, easy, and unobtrusive, so it doesn’t detract from the enjoyment of your app.

Whenever possible, support biometric authentication. Face ID and Touch ID are secure, familiar authentication methods that people trust. Hmmm !!.. That sounds convincing. Let’s see how we can add TouchID and FaceID into an app.

First job is to import the LocalAuthentication to the app. Then we have create an LAContext object , which will provide a UI for evaluating the authentication policies and access controls, managing credentials, and invalidating authentication contexts.

Don’t forget to add the #available(iOS 8.0, macOS 10.12.1, *) before adding any code for TouchID/FaceID authentication. They are not supported below iOS 8. After adding the case we have to check whether a particular policy can be evaluated, here its device authentication with bio-metrics. When that block is complete we can run the evaluatePolicy to get user authentication/consent. Once user authenticate go ahead and modify the UI or do your action.

Wait, after a successful authentication if you gonna update the UI part, remember always use DispatchQueue.main.async to run those tasks. The UI Changes must run in main thread.

Here is the code,

//This framework contains authentication helper codes
import LocalAuthentication
class ViewController: UIViewController { @IBAction func touchIdAction(_ sender: UIButton) {

print("hello there!.. You have clicked the touch ID")

let myContext = LAContext()
let myLocalizedReasonString = "Biometric Authntication testing !! "

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in

DispatchQueue.main.async {
if success {
// User authenticated successfully, take appropriate action
self.successLabel.text = "Awesome!!... User authenticated successfully"
} else {
// User did not authenticate successfully, look at error and take appropriate action
self.successLabel.text = "Sorry!!... User did not authenticate successfully"
}
}
}
} else {
// Could not evaluate policy; look at authError and present an appropriate message to user
successLabel.text = "Sorry!!.. Could not evaluate policy."
}
} else {
// Fallback on earlier versions

successLabel.text = "Ooops!!.. This feature is not supported."
}


}
}

Try testing in a real device. If you don’t have a real device no worries, use the Simulator’s Hardware->Touch ID options.

You can find the complete project in github

Now your turn , try it , happy Coding..💻👍►

--

--