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:
- Import Local Authentication framework
- Create Local Authentication Context – LAContext
- Determine of Biometric Sensor is available on user device and if Touch ID can be used
- Use evaluatePolicy() function to determine if user is a device owner
- 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.
[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.