Local Authentication in iOS
In a Nutshell, Local Authentication in iOS is a framework which we use to validate genuine user through passphrase (i.e. passcode) or TouchID. TouchID and RetinaID belong to biometric criteria. So, If you have got a requirement to integrate a User Validation then use this Local Authentication. Or, You can also invent the complete new wheel, but in such cases, you’re assuming your user should remember the authentication code/pattern of your app which is not a good idea.
LocalAuthentication.swift
Don’t panic, it’s just a wrapper file around Local Authentication ‘s behavior to create an abstraction. You just need to access authenticate(handler:) method of this class to authenticate the user. Yes, a single method will do the task for you.
If You want to use in any of your projects, just add this file and follow single method call.
LocalAuthentication.authenticate { (hasAuthenticated, error) in if hasAuthenticated { print("Success") } else { print(error) } }
It is obvious Authentication can fail. And, there are various reasons to fail. So, what are they?
1. AppCancel
– Authentication was canceled by the application (e.g. invalidate was called while
authentication was in progress).
2. Authentication failed
– Authentication was not successful because user failed to provide valid credentials.
3. Biometry Lockout
– Authentication was not successful because there were too many failed biometry attempts and biometry is now locked. Passcode is required to unlock biometry
4. Biometry Not Available
– Authentication could not start because biometry is not available on the device.
5. Biometry Not Enrolled
– Authentication could not start because biometry has no enrolled identities.
6. Passcode Not Set
– Authentication could not start because passcode is not set on the device.
7. System Cancel
– Authentication was canceled by the system (e.g. another application went to foreground).
8. User Cancel
– Authentication was canceled by the user (e.g. tapped Cancel button).
9. User Fallback
– Authentication was canceled because the user tapped the fallback button (Enter Password).
But, in the authenticate(handler:) method of LocalAuthentication.swift you will get a type of AuthenticationError which is a custom enum type.
Source Code
https://github.com/greenSyntax/ios-source/tree/master/LocalAuthentication