Use Touch ID fingerprint to unlock and use Touch ID fingerprint to unlock
Touch ID is a new feature added after iPhone 5 s, which is a well-known fingerprint recognition technology. In most cases, you may need to perform screen decoding on your mobile phone. You do not need to enter the four-digit password of your mobile phone for verification as before. On the one hand, you don't have to worry about the password being seen by others, and on the other hand, it facilitates your operations. After iOS 8, Apple opened the Touch id api for developers, which also brought a new experience to our apps.
Use
The Touch id api is very easy to understand. before using it, we need to import the LocalAuthentication library. Key modules are introduced: LAcontext
Apple is not fully open to Touch ID, but has opened the following two interfaces:
(1) canEvaluatePolicy: error: identify whether Touch ID can be authenticated
(2) evaluatePolicy: localizedReson: reply: authentication Touch ID
The key code is as follows:
LAContext * myContext = [[LAContext alloc] init];
NSError * authError = nil;
NSString * mylodomainzedreasonstring = @ "scan your fingerprint .";
If ([myContext canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error: & authError]) {
[MyContext evaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason: myLocatizedReasonString reply: ^ (BOOL success, NSError * _ Nullable error ){
If (success ){
Dispatch_async (dispatch_get_main_queue (), ^ {
// Update the UI in the main thread. Otherwise, the UI will become stuck.
TouchViewController * touchVC = [[TouchViewController alloc] init];
[Self. navigationController pushViewController: touchVC animated: YES];
});
}
If (! Success ){
NSLog (@ "% @", error );
}
}];
}
Error Handling
Typedef NS_ENUM (NSInteger, LAError)
{
// User verification fails. For example, an incorrect finger fingerprint is provided.
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,
// The user has canceled Touch ID verification.
LAErrorUserCancel = kLAErrorUserCancel,
// You do not want to perform Touch ID verification or enter a password.
LAErrorUserFallback = kLAErrorUserFallback,
// Verification terminated
LAErrorSystemCancel = kLAErrorSystemCancel,
// The user has not set the password in the device Settings
LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,
// The device does not support Touch ID
LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,
// No Touch ID fingerprint is registered on the device
LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,
} NS_ENUM_AVAILABLE (10_10, 8_0 );