Use iOS 8 SDK to add Touch ID fingerprint recognition
The iOS 8 SDK exposes the Touch ID fingerprint recognition feature to developers, allowing apps to locally verify user identities. It is very easy to use Touch ID. It only takes two steps:
1. Check whether the Touch ID is available.
2. Obtain the fingerprint verification result.
The following is a simple example to illustrate how to use Touch ID.
Create a new project and add a button to the interface designer. To use Touch ID, you need to introduce the LocalAuthentication framework:
import LocalAuthentication
In the Touch Up Inside Event of the button, we first check whether the Touch ID function is available:
@ IBAction func useTouchIdButtonClicked (sender: AnyObject) {// Step 1: Check whether Touch ID is available let authenticationContext = LAContext () var error: NSError? Let isTouchIdAvailable = authenticationContext. canEvaluatePolicy (. DeviceOwnerAuthenticationWithBiometrics, error: & error) if isTouchIdAvailable {NSLog ("congratulations, Touch ID can be used! ") // Step 2: Obtain the fingerprint verification result authenticationContext. evaluatePolicy (. deviceOwnerAuthenticationWithBiometrics, localizedReason: "You need to verify your fingerprint to confirm your identity information", reply: {(success, error)-> Void in if success {NSLog ("congratulations, you have passed the Touch ID fingerprint verification! ")} Else {NSLog (" sorry, you failed to pass Touch ID fingerprint verification! \ N \ (error) ")} else {NSLog (" sorry, Touch ID cannot be used! \ N \ (error )")}}
The above code first creates a LAContext instance for executing Authentication Policies ). Then, the canEvaluatePolicy method is called on the object to execute a specified authentication policy. The method signature is:
func canEvaluatePolicy(policy: LAPolicy, error: NSErrorPointer) -> Bool
Currently, the LAPolicy enumeration has only one enumeration value. DeviceOwnerAuthenticationWithBiometrics, which is used to authenticate the Device Host using fingerprint biometric identification.
enum LAPolicy: Int{ case DeviceOwnerAuthenticationWithBiometrics}
Note that the canEvaluatePolicy method returns the Bool value, indicating whether the specified authentication policy can be executed. When the method returns false, you can use the error object to obtain the detailed cause of failure. The failure may be caused by the failure of the device itself, such as the iPhone and iPad of the old version, running on the simulator, or the user has not enabled the Touch ID function.
For example, the running result on iPhone 5 is:
Sorry, the Touch ID cannot be used! Optional (Error Domain = com. apple. LocalAuthentication Code =-6 "Biometry is not available on this device." UserInfo = 0x15ec5a00 {NSLocalizedDescription = Biometry is not available on this device .})
The running result on the simulator is:
Sorry, the Touch ID cannot be used! Optional (Error Domain = com. apple. LocalAuthentication Code =-1000 "Simulator is not supported." UserInfo = 0x7ffe604b0790 {NSLocalizedDescription = Simulator is not supported .})
When Touch ID is allowed, you can call the evaluatePolicy method to execute the specified authentication policy. The method signature is:
func evaluatePolicy(policy: LAPolicy, localizedReason: String!, reply: ((Bool, NSError!) -> Void)!)
When this method is called, The system call Touch ID dialog box is displayed. The localizedReason parameter is used to prompt the user for detailed reasons and causes (concise but not empty or empty strings) in the dialog box ).
The reply parameter is a Block. The Bool type parameter success indicates whether fingerprint verification is successful. When a failure occurs, the error parameter contains the specific failure information. There are many failures here (I have tested only the following 5 cases, please let us know if there are any omissions ):
1. Results of three consecutive fingerprint recognition errors:
Sorry, you failed to pass the Touch ID fingerprint verification! Error Domain = com. apple. LocalAuthentication Code =-1 "Aplication retry limit exceeded." UserInfo = 0x1740797c0 {NSLocalizedDescription = Aplication retry limit exceeded .}
2. The Touch ID function is locked. The running result of the next time you need to enter the system password:
Sorry, you failed to pass the Touch ID fingerprint verification! Error Domain = com. apple. LocalAuthentication Code =-1 "Biometry is locked out." UserInfo = 0x17407dc00 {NSLocalizedDescription = Biometry is locked out .}
3. the user clicks the cancel button in the Touch ID dialog box:
Sorry, you failed to pass the Touch ID fingerprint verification! Error Domain = com. apple. LocalAuthentication Code =-2 "Canceled by user." UserInfo = 0x17006c780 {NSLocalizedDescription = Canceled by user .}
4. When the Touch ID dialog box is displayed, the background system is canceled. For example, press the power key:
Sorry, you failed to pass the Touch ID fingerprint verification! Error Domain = com. apple. LocalAuthentication Code =-4 "UI canceled by system." UserInfo = 0x170065900 {NSLocalizedDescription = UI canceled by system .}
5. In the Touch ID dialog box, click the Enter Password button:
Sorry, you failed to pass the Touch ID fingerprint verification! Error Domain = com. apple. LocalAuthentication Code =-3 "Fallback authentication mechanism selected." UserInfo = 0x17407e040 {NSLocalizedDescription = Fallback authentication mechanism selected .}
Through this simple example, we believe you have learned how to add the fingerprint recognition function to your App. The project code in this article:
Click Open Link
If you have any questions, please leave a message.