1. Add NSFaceIDUsageDescription permission application description to info.plist
<key>NSFaceIDUsageDescription</key> <string> Allows the device to access FaceID</string>Copy the code
Github integrates YZAuthID
3. Reference code
#import "YZAuthID.h"
[self authVerification];
Copy the code
- (void)authVerification {
YZAuthID *authID = [[YZAuthID alloc] init];
[authID yz_showAuthIDWithDescribe:nil block:^(YZAuthIDState state, NSError *error) {
if(state == YZAuthIDStateNotSupport) {TouchID/FaceID NSLog(@) is not supported"Sorry, fingerprint/face ID is not supported on the current device");
} else if(state == YZAuthIDStateFail) {// Authentication failed NSLog(@"Fingerprint/face ID incorrect, authentication failed.");
} else if(state = = YZAuthIDStateTouchIDLockout) {/ / many errors, has been locked NSLog (@"Repeated errors, fingerprint/face ID has been locked, please enter your password in the unlock screen of your phone.");
} else if(state == YZAuthIDStateSuccess) {// TouchID/FaceID authentication success NSLog(@"Certification successful!"); }}]; }Copy the code
4. Effect display
#import <LocalAuthentication/LocalAuthentication.h>/** * TouchID/FaceID state */ typedef NS_ENUM(NSUInteger, TouchID/FaceID */ YZAuthIDStateNotSupport = 0, /** ** TouchID/FaceID authentication succeeded */ YZAuthIDStateSuccess = 1, /** ** TouchID/FaceID authentication failed */ YZAuthIDStateFail = 2, /** ** TouchID/FaceID manually cancelled */ YZAuthIDStateUserCancel = 3, / * * * the user does not use TouchID/FaceID, choose to manually input password * / YZAuthIDStateInputPassword = 4, / * * * TouchID/FaceID system be abolished (such as telephone, lock screen, press the Home button, etc.) * / YZAuthIDStateSystemCancel = 5, / * * * TouchID/FaceID cannot be started, because the user does not have a password * / YZAuthIDStatePasswordNotSet = 6, / * * * TouchID/FaceID cannot be started, because the user is not set TouchID/FaceID * / YZAuthIDStateTouchIDNotSet = 7, / * * * TouchID/FaceID void * / YZAuthIDStateTouchIDNotAvailable = 8, / * * * TouchID/FaceID locked (validation TouchID/FaceID failure many times continuously, the system requires the user to manually enter password) * / YZAuthIDStateTouchIDLockout = 9, /** * YZAuthIDStateAppCancel = 10, / * * * the current software that has been suspended and cancel the authorization (LAContext object is invalid) * / YZAuthIDStateInvalidContext = 11, / * * * system version does not support TouchID/FaceID (must be higher than the iOS 8.0 to use) * / YZAuthIDStateVersionNotSupport = 12}; @interface YZAuthID : LAContext typedef void (^YZAuthIDStateBlock)(YZAuthIDState state, NSError *error); /** * start TouchID/FaceID for verification * @param describe TouchID/FaceID display description * @param block callback status block */ - (void)yz_showAuthIDWithDescribe:(NSString *)describe block:(YZAuthIDStateBlock)block; @endCopy the code
YZAuthID.m
#import "YZAuthID.h"
#import <UIKit/UIKit.h>
#define iPhoneX (UIScreen.mainScreen.bounds.size.width >= 375.f && UIScreen.mainScreen.bounds.size.height >= 812.f)
@implementation YZAuthID
+ (instancetype)sharedInstance {
static YZAuthID *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[YZAuthID alloc] init];
});
return instance;
}
- (void)yz_showAuthIDWithDescribe:(NSString *)describe block:(YZAuthIDStateBlock)block {
if(! describe) {if(iPhoneX){
describe = @"Verify your face.";
}else{
describe = @"Verify fingerprint with Home button"; }}if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"System version does not support TouchID/FaceID (must be higher than iOS 8.0 to use)");
block(YZAuthIDStateVersionNotSupport, nil);
});
return; } LAContext *context = [[LAContext alloc] init]; // An authentication failure message is displayed as @""Don't tip context. LocalizedFallbackTitle = @"Enter your password"; NSError *error = nil; / / LAPolicyDeviceOwnerAuthenticationWithBiometrics: TouchID FaceID validation / / LAPolicyDeviceOwnerAuthentication: Verify with TouchID/FaceID or password, default is error twice or lock, pop up enter password interface (used in this case)if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:describe reply:^(BOOL success, NSError * _Nullable error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID verification successful");
block(YZAuthIDStateSuccess, error);
});
}else if(error){
if(@available(iOS 11.0, *)) {switch (error.code) {case LAErrorAuthenticationFailed:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID verification failed");
block(YZAuthIDStateFail, error);
});
break;
}
case LAErrorUserCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID was manually cancelled by user");
block(YZAuthIDStateUserCancel, error);
});
}
break;
case LAErrorUserFallback:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"User does not use TouchID/FaceID and chooses to enter password manually.");
block(YZAuthIDStateInputPassword, error);
});
}
break;
case LAErrorSystemCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID is cancelled by the system (e.g. incoming call, screen locked, Home button pressed, etc.)");
block(YZAuthIDStateSystemCancel, error);
});
}
break;
case LAErrorPasscodeNotSet:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID cannot start because the user has not set a password");
block(YZAuthIDStatePasswordNotSet, error);
});
}
break;
//case LAErrorTouchIDNotEnrolled:{
case LAErrorBiometryNotEnrolled:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID cannot be started because the user has not set TouchID/FaceID");
block(YZAuthIDStateTouchIDNotSet, error);
});
}
break;
//case LAErrorTouchIDNotAvailable:{
case LAErrorBiometryNotAvailable:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID invalid");
block(YZAuthIDStateTouchIDNotAvailable, error);
});
}
break;
//case LAErrorTouchIDLockout:{
case LAErrorBiometryLockout:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID/FaceID is locked (multiple attempts to verify TouchID/FaceID fail, system requires user to manually enter password)");
block(YZAuthIDStateTouchIDLockout, error);
});
}
break;
case LAErrorAppCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"The current software is suspended and unlicensed (e.g. the App entered the background, etc.)");
block(YZAuthIDStateAppCancel, error);
});
}
break;
case LAErrorInvalidContext:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Current software is suspended and unlicensed (invalid LAContext object)");
block(YZAuthIDStateInvalidContext, error);
});
}
break;
default:
break; }}else{// iOS 11.0 or later only TouchID authentication switch (error.code) {case LAErrorAuthenticationFailed:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID verification failed");
block(YZAuthIDStateFail, error);
});
break;
}
case LAErrorUserCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID was manually cancelled by the user.");
block(YZAuthIDStateUserCancel, error);
});
}
break;
case LAErrorUserFallback:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"User doesn't use TouchID, chooses to manually enter password.");
block(YZAuthIDStateInputPassword, error);
});
}
break;
case LAErrorSystemCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID has been cancelled by the system (e.g. incoming call, screen locked, Home button pressed, etc.)");
block(YZAuthIDStateSystemCancel, error);
});
}
break;
case LAErrorPasscodeNotSet:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID cannot start because the user has not set a password.");
block(YZAuthIDStatePasswordNotSet, error);
});
}
break;
case LAErrorTouchIDNotEnrolled:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID cannot start because the user has not set TouchID.");
block(YZAuthIDStateTouchIDNotSet, error);
});
}
break;
//case: {case LAErrorTouchIDNotAvailable:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID invalid");
block(YZAuthIDStateTouchIDNotAvailable, error);
});
}
break;
case LAErrorTouchIDLockout:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"TouchID locked (failed to verify TouchID multiple times, system requires user to manually enter password)");
block(YZAuthIDStateTouchIDLockout, error);
});
}
break;
case LAErrorAppCancel:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"The current software is suspended and unlicensed (e.g. the App entered the background, etc.)");
block(YZAuthIDStateAppCancel, error);
});
}
break;
case LAErrorInvalidContext:{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Current software is suspended and unlicensed (invalid LAContext object)");
block(YZAuthIDStateInvalidContext, error);
});
}
break;
default:
break; }}}}]; }else{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Current device does not support TouchID/FaceID");
block(YZAuthIDStateNotSupport, error);
});
}
}
@end
Copy the code