Touch ID Implementation in iOS Application
Posted By : Prince Verma | 09-Sep-2018
Apple introduced Touch ID in iPhone 5s and above versions. Touch ID basically a layer of security you can use to unlock your phone and Apple has provided this feature to use in your Application. This makes your application secure and smart. Instead of using a password you can directly perform actions by simply giving your registered fingerprint.
This feature can be implemented in iOS 8.0 + version. Apple didn't provide this in lower versions of iPhone.
To implement this, we have to understand the Local Authentication Framework.
Local Authentication: Local Authentication is a framework given by Apple to implement Touch Id in your application. To maximise security, Apple has built a hardware-based security processor isolated from the rest of the system which is named as "SECURITY ENCLAVE". It manages the fingerprint data out of reach even of Operating system.
It checks for authentication and returns boolean.
Let's assume that you have to login with Touch Id.
Make an IBAction of Login button.
Inside IBAction, make an object of LAContext.
let authContext = LAContext()
let authReason = "Please use touch ID to sign in."
var authError:NSError?
// Return true if biometric will get succeeded
if authContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &authError){
authContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: authReason) { (success, error) in
if success{
print("authenticate successfully")
// perform logic to go ahead
DispatchQueue.main.async(execute: {
// Login Successful
// main thread to perform something
})
}
else{
DispatchQueue.main.async(execute: {
self.reportTouchIDError(error: error as! NSError)
})
}
}
= }else{
// We have an error
print(authError?.localizedDescription)
//Show login screen again
}
// Touch Id error scenarios
func reportTouchIDError(error:NSError){
switch error.code{
case LAError.authenticationFailed.rawValue:
print("Authentication failed")
case LAError.passcodeNotSet.rawValue:
print("Authentication failed")
case LAError.systemCancel.rawValue:
print("Authentication failed")
case LAError.userCancel.rawValue:
print("Authentication failed")
case LAError.userFallback.rawValue:
print("Authentication failed")
default:
print(error.localizedDescription)
}
}
OUTPUT:
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Verma
Prince Verma is working as an iOS Developer. He is very dedicated and hardworking towards his work. His interest includes Research and Development.