SoFunction
Updated on 2025-04-03

Simple implementation of iOS fingerprint unlocking (TouchID)

This article has shared the specific code for iOS fingerprint unlocking for your reference. The specific content is as follows

1. First, introduce a dependency framework

#import <LocalAuthentication/>

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

//The fingerprint recognition interface is only supported after iOS 8.0 if ([UIDevice currentDevice]. &lt; 8.0) {
  return;
 }

3. Finally, call the following method when APP starts to complete the integration of all functions of fingerprint unlocking

- (void)evaluateAuthenticate
{
 //Create LAContext LAContext* context = [[LAContext alloc] init];
 NSError* error = nil;
 NSString* result = @"Please verify that you have fingerprints";
 
 //First use canEvaluatePolicy to determine the device support status if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&amp;error]) {
  //Support fingerprint verification  [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
   if (success) {
    //The verification is successful, the main thread handles the UI   }
   else
   {
    NSLog(@"%@",);
    switch () {
     case LAErrorSystemCancel:
     {
      //The system cancels the authorization, such as other APPs entering      break;
     }
     case LAErrorUserCancel:
     {
      //User unverified Touch ID      break;
     }
     case LAErrorAuthenticationFailed:
     {
      //Authorization failed      break;
     }
     case LAErrorPasscodeNotSet:
     {
      //The system does not set a password      break;
     }
     case LAErrorTouchIDNotAvailable:
     {
      //The device Touch ID is not available, for example not opened      break;
     }
     case LAErrorTouchIDNotEnrolled:
     {
      //The device Touch ID is not available and the user has not entered it      break;
     }
     case LAErrorUserFallback:
     {
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
       //The user selects to enter the password and switches to the main thread processing       
      }];
      break;
     }
     default:
     {
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
       //Other situations, switch main thread processing      }];
      break;
     }
    }
   }
  }];
 }
 else
 {
  //No fingerprint recognition is supported, LOG error details  NSLog(@"Fingerprint recognition is not supported");
  
  switch () {
   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(@"%@",);
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.