iOS - calling Face ID and Touch ID in application

Posted by liquorvicar on Sun, 06 Mar 2022 04:52:17 +0100

Note that many non-native iOS applications call Face ID in the application to assist login and confirm privacy operations. Here is how to call Face ID or Touch ID.

Get user privacy

Similar to calling location and camera, first in info Add Face ID permission in plist

Privacy - Face ID Usage Description

Import header file

Import library files of Face ID and Touch ID

#import <LocalAuthentication/LocalAuthentication.h>

Core method

Judge whether the device supports Face ID or Touch ID

- (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error __attribute__((swift_error(none))) API_AVAILABLE(macos(10.10), ios(8.0), watchos(3.0), tvos(10.0));

Verify the Face ID or Touch ID, and a pop-up window will appear

- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError * __nullable error))reply
    API_AVAILABLE(macos(10.10), ios(8.0), watchos(3.0), tvos(10.0));

Core code

- (void)faceID {
    
    //Create LAContext
    LAContext *context = [[LAContext alloc] init];
    
    //This property is the option to set the pop-up box after biometric authentication fails
    context.localizedFallbackTitle = @"Login with account and password";
    
    //error message
    NSError *error = nil;
    //Judge whether the device supports Face ID or Touch ID
    BOOL isUseFaceOrTouchID = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    if (isUseFaceOrTouchID) {
        //This is used to verify the TouchID, and a pop-up box will appear
        //The string parameter is the prompt when validation fails
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Verification failed! Maybe you...not oneself?" reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"Verification successful");
                });
                
            } else {
                NSLog(@"%@", error.localizedDescription);
                switch (error.code) {
                    case LAErrorSystemCancel: {
                        NSLog(@"The system cancels authorization, such as others APP cut-in");
                        break;
                    }
                    case LAErrorUserCancel: {
                        NSLog(@"User cancels authentication Face ID");
                        break;
                    }
                    case LAErrorAuthenticationFailed: {
                        NSLog(@"privilege grant failed");
                        break;
                    }
                    case LAErrorPasscodeNotSet: {
                        NSLog(@"The system does not set a password");
                        break;
                    }
                    case LAErrorBiometryNotAvailable: {
                        NSLog(@"equipment Face ID Not available, e.g. not open");
                        break;
                    }
                    case LAErrorBiometryNotEnrolled: {
                        NSLog(@"equipment Face ID Not available, not entered by user");
                        break;
                    }
                    case LAErrorUserFallback: {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"User input password");
                        }];
                        break;
                    }
                    default: {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"In other cases, switch the main thread processing");
                        }];
                        break;
                    }
                }
            }
        }];
        
    } else {
        NSLog(@"I won't support it Face ID or Touch ID");
        switch (error.code) {
            case LAErrorBiometryNotEnrolled: {
                NSLog(@"Face ID unregistered");
                break;
            }
            case LAErrorPasscodeNotSet: {
                NSLog(@"No password set");
                break;
            }
            default: {
                NSLog(@"Face ID Not available");
                break;
            }
        }
        
        NSLog(@"%@",error.localizedDescription);
    }
    
}

Verification failure effect

As can be seen in the core code, there are many error conditions. We can add the desired operation to the required error type case, remind the user of the error type, or directly cancel the call of Face ID or Touch ID.

Aside about biometric assisted landing

When developing login retention, we can use persistent data to store the marks of successful login. However, for apps with high security, the login status will be verified every time they are restarted. It is not enough to rely on persistent data alone.
We can take the rider side of takeout software as an example: suppose that the rider side of our project needs face verification to verify whether it is himself. The rider successfully logs in for the first time and enables biometric verification - > after exiting the App - > starting the App for the second time, how to judge whether to enable biometric login verification?

First of all, we can verify the identity of riders every certain time, such as 3 hours and half a day, so as to ensure the food safety of users as much as possible on the premise of not interfering with the work of riders.
Other solutions have also been found online, such as judging through multiple BOOL type design logic:

It can be seen that using biometric verification to assist login seems simple. To write reasonable code, we also need to carry out logical design according to the specific functions of the product.

Topics: iOS xcode objective-c