Using the iOS 8 SDK to add Touch ID fingerprint recognition

Source: Internet
Author: User

The IOS 8 SDK exposes Touch ID fingerprinting to developers, allowing the app to authenticate users locally. Using Touch ID is simple and requires only 2 steps:

1. Check if Touch ID is available.

2. Obtain fingerprint verification results.

Here's a simple example of how to use touch ID.

Create a new project and add a button to the interface designer. Using Touch ID requires the introduction of the Localauthentication framework:

Import Localauthentication

In the touch up inside event of the button we first check whether the Touch ID feature is available:

    @IBAction func usetouchidbuttonclicked (sender:anyobject) {//step 1: Check if Touch ID is available let Authenticationco        ntext = Lacontext () var error:nserror? Let istouchidavailable = Authenticationcontext.canevaluatepolicy (.            Deviceownerauthenticationwithbiometrics, Error: &error) if istouchidavailable { NSLog ("Congratulations, Touch ID can be used!") ")//Step 2: Get fingerprint verification result Authenticationcontext.evaluatepolicy (. Deviceownerauthenticationwithbiometrics, Localizedreason: "Need to verify your fingerprint to confirm your identity", reply: {(success, error)-& Gt Void in If success {NSLog ("Congratulations, you passed the Touch ID fingerprint verification! ")} else {NSLog (" Sorry, you failed to authenticate with Touch ID fingerprint! \n\ (Error)}})} else {NSLog ("Sorry, Touch ID is not available! \n\ (Error)}}

The above code first creates a Lacontext instance that performs the authentication policy (authentication policies). The Canevaluatepolicy method is then invoked on the object to execute a specified authentication policy with the method signature:

Func Canevaluatepolicy (Policy:lapolicy, error:nserrorpointer), Bool

The Lapolicy enumeration currently has only one enumeration value. Deviceownerauthenticationwithbiometrics, that is, the use of fingerprint biometric identification method to authenticate the machine master.

Enum lapolicy:int{case    Deviceownerauthenticationwithbiometrics}
Note that the Canevaluatepolicy method returns a bool value indicating whether the specified authentication policy is allowed to execute. When the method returns false, the Error object can be used to obtain a detailed reason for the failure. The failure may not be supported by the device itself, such as an older version of the iphone and ipad, running on the emulator, or the user not turning on the Touch ID feature.

For example, running the result on iphone 5 is:

Sorry, Touch ID is not available! 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 result of the operation on the emulator is:

Sorry, Touch ID is not available! 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 then call the EvaluatePolicy method to execute the specified authentication policy with the method signature:

Func EvaluatePolicy (Policy:lapolicy, localizedreason:string!, Reply: ((Bool, nserror!), Void)!)
Calling this method will bring up the system to invoke the Touch ID dialog box, where the Localizedreason parameter is used to prompt the user for detailed reasons and reasons in the dialog box (should be concise, but not empty or empty string).

The reply parameter is a block in which the bool type parameter success indicates whether the fingerprint validation passed, and when it fails, the error parameter contains the specific failure information. There are a number of failure scenarios (I have only tested the following 5 cases, if omitted, please inform):

1. Running results of three consecutive fingerprint recognition errors:

Sorry, you failed to authenticate with Touch ID fingerprint! Error domain=com.apple.localauthentication code=-1 "aplication retry limit exceeded." userinfo=0x1740797c0 {nslocalizeddescription=aplication retry limit exceeded.}
2. Touch ID function is locked, the next time you need to enter the system password operation results:
Sorry, you failed to authenticate with Touch ID fingerprint! Error domain=com.apple.localauthentication code=-1 "Biometry is locked out." USERINFO=0X17407DC00 {Nslocalizeddescription=biometry is locked out.}
3. The user clicked the Cancel button in the Touch ID dialog box:

Sorry, you failed to authenticate with Touch ID fingerprint! Error domain=com.apple.localauthentication code=-2 "Canceled by user." userinfo=0x17006c780 {nslocalizeddescription=canceled by user.}
4. During the Touch ID dialog display, the back system is canceled, such as pressing the Power key:

Sorry, you failed to authenticate with Touch ID fingerprint! Error domain=com.apple.localauthentication code=-4 "UI canceled by System." userinfo=0x170065900 {Nslocalizeddescription=ui canceled by system.}
5. The user clicks the Enter Password button in the Touch ID dialog box:

Sorry, you failed to authenticate with Touch ID fingerprint! Error domain=com.apple.localauthentication code=-3 "Fallback authentication mechanism selected." userinfo=0x17407e040 {nslocalizeddescription=fallback authentication mechanism selected.}
With this simple example, I believe you already know how to add fingerprint recognition to your app. Project code for this article:

Click to open link

If you have any questions, please feel free to leave me a message.

Using the iOS 8 SDK to add Touch ID fingerprint recognition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.