Touch ID Authentication in Swift
Apple introduce TouchID in which instead of entering password, user can unlock the their device using their fingerprint. In this tutorial we are going to learn how to use this functionality in iOS Application.
Let’s Start
- Open Xcode and create a new project
2. Enter product name and fill other details
Once you fill all the details then press Next
3. Open Main.storyboard and drag a button to the View Controller Scene. Set the title of the button to Authenticate and add constraints i.e horizontally and vertically in container to button as shown in picture
4. Select the Assistant Editor and make sure the ViewController.swift is visible. Control and drag from the Button to the ViewController class and create the action as shown in picture
5. Go to the ViewController.swift file and import LocalAuthentication framework
import LocalAuthentication
Next add a helper method to display messages using UIAlertController
func displayAlert(title: String = "", message: String) {let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)alertController.addAction(UIAlertAction.init(title: "Ok", style: .default, handler: { (action) in}))self.present(alertController, animated: true, completion: nil)}
6. Implement the authenticateUserWithTouchID method and call this method in Authenticate button action
func authenticateUserWithTouchID() {let context : LAContext = LAContext()let myLocalizedReasonString = "Authenticate with Touch ID"var authError: NSError?if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: myLocalizedReasonString) { success, evaluateError inif success {self.displayAlert(message: "Touch ID Authentication is Successful")}else {self.displayAlert(message: "Touch ID Authentication is Failed")}}} else {self.displayAlert(message: "Touch ID not available")}}@IBAction func actionOnAuthenticateButton(_ sender: UIButton) {authenticateUserWithTouchID()}
Build and run the app on a real device, you will get following results
If you want to handle LAContext error and display proper error message to the user then you can use below code
func getErrorMessageForLAErrorCode(errorCode: Int) -> String {var message = ""switch errorCode {case LAError.appCancel.rawValue:message = "Authentication was canceled by application"case LAError.authenticationFailed.rawValue:message = "Authentication was not successful, because user failed to provide valid credentials."case LAError.invalidContext.rawValue:message = "The context is invalid"case LAError.passcodeNotSet.rawValue:message = "Authentication could not start, because passcode is not set on the device."case LAError.systemCancel.rawValue:message = "Authentication cancelled by the system"case LAError.userCancel.rawValue:message = "Authentication was canceled by user"case LAError.userFallback.rawValue:message = "Authentication was canceled, because the user tapped the fallback button"default:message = "Error code not found in LAError"}return message}
Thanks for reading 😊