iOS 8 uses Touch ID for authentication

Keywords: iOS

 

The minimum supported model of fingerprint identification function of iOS system is iPhone 5s, and the minimum supported system is iOS 8. Although the 5s model installed with iOS 7 system can use the fingerprint unlocking function provided by the system, the API is not open, so in theory, the third-party software cannot be used.

 

Dependency framework LocalAuthentication.framework

#import <LocalAuthentication/LocalAuthentication.h>

 

When adapting iOS 8 and below, it is necessary to verify the API to avoid calling related API and causing crash.

// Used to determine whether the device supports Touch ID
  - (BOOL)canEvaluatePolicy:(LAPolicy)policy error:(NSError * __autoreleasing *)error;
// Functions that actually authenticate
  - (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError *error))reply;

If canEvaluatePolicy returns YES, indicating that the device supports fingerprint identification, then the evaluatePolicy function can be called for fingerprint identification. An alert pops up when the evaluatePolicy function is called.

If the localizedFallbackTitle property of LAContext is not set, the default is "Enter Password". If the property is set to @ "" (empty string), the button will be hidden.

 

- (void)authenticateUser
{
    //Initialize context object
    LAContext* context = [[LAContext alloc] init];
    //Wrong object
    NSError* error = nil;
    NSString* result = @"Authentication is needed to access your notes.";
    
    //First, canEvaluatePolicy is used to determine the device support status
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        //Support fingerprint verification
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
            if (success) {
                //Verification successful, main thread processing UI
            }
            else
            {
                NSLog(@"%@",error.localizedDescription);
                switch (error.code) {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"Authentication was cancelled by the system");
                        //Switch to another APP, the system cancels the verification of Touch ID
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"Authentication was cancelled by the user");
                        //User revoking Touch ID
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        NSLog(@"User selected to enter custom password");
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //User chooses to input password and switch main thread processing
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            //In other cases, switch main thread processing
                        }];
                        break;
                    }
                }
            }
        }];
    }
    else
    {
        //Fingerprint recognition is not supported, LOG gives error details
        
        switch (error.code) {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
        
        NSLog(@"%@",error.localizedDescription);
        [self showPasswordAlert];
    }
}

//privilege grant failed
LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,

//User cancels Touch ID authorization
LAErrorUserCancel           = kLAErrorUserCancel,

//User chooses to enter password
LAErrorUserFallback         = kLAErrorUserFallback,

//System de authorization (e.g. other APP entry)
LAErrorSystemCancel         = kLAErrorSystemCancel,

//Password not set by system
LAErrorPasscodeNotSet       = kLAErrorPasscodeNotSet,

//Device Touch ID is not available, for example not open
LAErrorTouchIDNotAvailable  = kLAErrorTouchIDNotAvailable,

//Device Touch ID not available, user did not enter
LAErrorTouchIDNotEnrolled   = kLAErrorTouchIDNotEnrolled,

 

Posted by grandman on Thu, 02 Jan 2020 19:56:04 -0800