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.")
            return
        }
        
        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.


Learn iOS Development with these Video Courses

The Complete iOS 11 & Swift Developer Course - Build 20 Apps

Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more. Preview this video course. icon

How to Make a Freaking iPhone App - iOS 11 and Swift 4

iPhone App Development from scratch. Learn how to make iOS apps using Xcode 9. The Basics Include Pokemon Go & Snapchat. Learn to build iOS Apps with Xcode 9 and Swift 4. How to Make a Freaking iPhone App - iOS 11 and Swift 4 icon

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C. Preview this video course. iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course icon