iOS fingerprint unlock verifies TouchID function

  • 2021-11-13 18:28:41
  • OfStack

1. Firstly, the dependency framework LocalAuthentication. framework is introduced


#import <LocalAuthentication/LocalAuthentication.h>

2. Then, whether the system is iOS8 or above is judged


//iOS8.0 Fingerprint identification interface is supported after 
  if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
    return;
  }

3. Finally, when APP is started, all functions of fingerprint unlocking can be integrated by calling the following methods


- (void)evaluateAuthenticate
{
  // Create LAContext
  LAContext* context = [[LAContext alloc] init];
  NSError* error = nil;
  NSString* result = @" Please verify that there are fingerprints ";
  // First use canEvaluatePolicy  Determine the equipment support status 
  if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
    // Support fingerprint verification 
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
      if (success) {
        // Verification is successful, and the main thread handles UI
      }
      else
      {
        NSLog(@"%@",error.localizedDescription);
        switch (error.code) {
          case LAErrorSystemCancel:
          {
            // The system cancels authorization, such as other APP Cut in 
            break;
          }
          case LAErrorUserCancel:
          {
            // User cancels authentication Touch ID
            break;
          }
          case LAErrorAuthenticationFailed:
          {
            // Authorization failure 
            break;
          }
          case LAErrorPasscodeNotSet:
          {
            // The system does not have a password set 
            break;
          }
          case LAErrorTouchIDNotAvailable:
          {
            // Equipment Touch ID Not available, such as not open 
            break;
          }
          case LAErrorTouchIDNotEnrolled:
          {
            // Equipment Touch ID Unavailable, not entered by user 
            break;
          }
          case LAErrorUserFallback:
          {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
              // The user chooses to enter the password and switches the main thread processing 
            }];
            break;
          }
          default:
          {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
              // In other cases, switch the main thread processing 
            }];
            break;
          }
        }
      }
    }];
  }
  else
  {
    // Fingerprint recognition is not supported, LOG Details of error 
    NSLog(@" Fingerprint recognition is not supported ");
    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);
  }
}

Related articles: