Touch ID Verification Code Example in Swift

With this short swift code example I am going to share with you how to do Touch ID Verification in Swift. This code example which covers:

  1. Import Local Authentication framework
  2. Create Local Authentication Context – LAContext
  3. Determine of Biometric Sensor is available on user device and if Touch ID can be used
  4. Use evaluatePolicy() function to determine if user is a device owner
  5. Handle Touch ID Local Authentication error messages

For this code to work you will need to Add LocalAuthentication.framework to your Xcode project and then import Local Authentication framework into your source code:

import LocalAuthentication

Touch ID Verification/Local Authentication Code Example in Swift

 //Create Local Authentication Context
        let authenticationContext = LAContext()
        var error:NSError?
        guard authenticationContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
            print("No Biometric Sensor Has Been Detected. This device does not support Touch Id.")
        authenticationContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Only device owner is allowed", reply: { (success, error) -> Void in
            if( success ) {
                print("Fingerprint recognized. You are a device owner!")
            } else {
                // Check if there is an error
                if let errorObj = error {
                    print("Error took place. \(errorObj.localizedDescription)")

Check out other useful code example in Swift for iOS development.

[raw_html_snippet id=”cookbookpagecoursesheader”]

Unit Testing Swift Mobile App

Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.