One, the introduction

After iPhone 5S, iPhone hardware has supported the function of fingerprint recognition. Accordingly, some new APIS can also be applied in APP for user security verification. Currently, developers can use two methods of security authentication, one is through the phone password authentication, and another is through fingerprint authentication.

2. Add security authentication for APP

To use security verification related API, we need to introduce the following header file: < > LocalAuthentication/LocalAuthentication. H

//typedef NS_ENUM(NSInteger, LAError)

/ / {

//// user authentication failed, such as providing the wrong finger fingerprint

//LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

//

//// user disabled Touch ID authentication

//LAErrorUserCancel= kLAErrorUserCancel,

//

//// the user does not want Touch ID authentication and wants to enter a password

//LAErrorUserFallback= kLAErrorUserFallback,

//

//// The system terminates authentication

//LAErrorSystemCancel= kLAErrorSystemCancel,

//

//// The user does not set a password in the device Settings

//LAErrorPasscodeNotSet= kLAErrorPasscodeNotSet,

//

//// The device does not support Touch ID

//LAErrorTouchIDNotAvailable= kLAErrorTouchIDNotAvailable,

//

//// The Touch ID fingerprint is not registered on the device

//LAErrorTouchIDNotEnrolled= kLAErrorTouchIDNotEnrolled,

//} NS_ENUM_AVAILABLE(10_10, 8_0);


Code 3.

// Initialize the context object

LAContext*context = [[LAContextalloc]init];

NSError*error;

NSString*result =@” need your payment to pay “;

The context. LocalizedFallbackTitle = @ “quick pay”;

// First use canEvaluatePolicy to determine the payment status of the device

if([contextcanEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricserror:&error]) {

// Support fingerprint verification

[contextevaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricslocalizedReason:resultreply:^(BOOLsuccess,NSError* _Nullableerror) {

if(success) {

// The main thread handles the UI

NSLog(@” verification successful “);

}else{

switch(error.code) {

caseLAErrorSystemCancel:

{

// Switch to another App, the system unvalidates Touch ID

}

break;

caseLAErrorUserCancel:

{

// User cancels Touch ID

}

break;

caseLAErrorUserFallback:

{

// The user chooses to enter the password to switch the main thread processing

[[NSOperationQueuemainQueue]addOperationWithBlock:^{

// Go back to the main thread for the activity

}];

}

break;

caseLAErrorAuthenticationFailed:

{// User authentication failed. Like providing the wrong fingerprint

}

break;

caseLAErrorTouchIDLockout:

{

}

break;

caseLAErrorAppCancel:

{

}

break;

caseLAErrorInvalidContext:

{

}

break;

default:

{

[[NSOperationQueuemainQueue]addOperationWithBlock:^{

// Other cases need to be handled in the main thread

}];

}

break;

}

}

}];

}else{

switch(error.code) {

caseLAErrorTouchIDNotEnrolled:

{

}

break;

caseLAErrorPasscodeNotSet:

{

}

break;

default:

{

// Touch ID is unavailable

}

break;

}

}

}