Brief introduction of fingerprint identification in IOS development

Time:2021-2-20

Fingerprint identification in IOS development is briefly introduced. In iPhone series, fingerprint identification function has been available since 5S, and fingerprint verification interface is open at IOS 8.

So we need to judge the model and system version when we apply fingerprint identification.

The code is as follows. In fact, what needs special attention is the difference between lapolicydeviceownerauthentication and lapolicydeviceownerauthentication with biometrics, and the version of the detection system passed through [uidevice currentdevice] systemVersion.floatValue Can evaluate policy: error: this method is used to judge whether the device is available for touch ID. It should also be noted that the following operations to verify the success of fingerprint identification are performed in the sub thread by default, so if we want to do UI operations, we need to return to the main thread to execute. Available utilizationdispatch_async(dispatch_queue_t _Nonnull queue, ^{ }) This function to achieve, which can be transferred to the main queue. In addition, we can also make some judgments according to the eror code to see what causes the user’s errors, and then make the corresponding output.

//1. Judge whether the system version is greater than or equal to 8.0. If it is greater than or equal to 8.0, fingerprint identification can be used
  if([UIDevice currentDevice].systemVersion.floatValue>=8.0)
  {
  //The function of fingerprint identification can be used only after 5S
   //Create the context of the La object
    LAContext * context = [[LAContext alloc]init];
    //Judge whether the device supports fingerprint identification
    //Evaluate means to evaluate
    //Policy represents a policy
    //Used to check whether the current device can use the touchid
    if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
    {
   //Lapolicydeviceownerauthentication: if we input the wrong fingerprint three times, the password box will pop up. If we don't input the password, the password box will pop up. If you come in again, you'll have two more chances to verify your fingerprints
    The error will continue to pop up the system password box for you to enter. If you do not enter the touch ID, it will be locked, while lapolicy device owner authentication with biology will not pop up the input system
    After three errors, no processing will be done by default. We can also click fingerprint recognition again to input, but if the error is still entered twice, the touch ID will be locked
     //Indicates that fingerprint identification technology can be used
      [context evalu atePolicy:LAPolicyDeviceOwnerAuthentication localizedReason : @ "please verify fingerprint for payment" reply:^
    (BOOL success, NSError * _Nullable error) {
        //It is executed in the sub thread, so if you want to update the UI, you must go back to the main thread to execute
        //Judge whether it is successful or not
        if(success)
        {
          NSLog(@"%@",[NSThread currentThread]);
          Nslog (@ "validation successful");
        }
        else
        {
          Nslog (@ "validation failed");
        }
        NSLog(@"%@",[NSThread currentThread]);
        NSLog(@"%@",error);
        if(error)
        {
         if(error.code==-2)
         {
         dispatch_async(dispatch_get_main_queue(), ^{
Uialertcontroller * VC = [uialertcontroller alertcontrollerwithtitle: @ "fingerprint verification cancelled" message: @ "
          preferredStyle:UIAlertControllerStyleAlert];
Uialertaction * action = [uialertaction actionwithtitle: @ "confirm" style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"---------");
}];
Uialertaction * action1 = [uialertaction actionwithtitle: @ "Cancel" style:UIAlertActionStyleCancel 
           handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"hhhhhh");
}];
[vc addAction:action];
[vc addAction:action1];
[self presentViewController:vc animated:YES completion:nil];
});
         }
         else if(error.code==-1)
         {
         dispatch_async(dispatch_get_main_queue(), ^{
Uialertcontroller * VC = [uialertcontroller alertcontrollerwithtitle: @ "fingerprint has been input incorrectly 3 times" message:
            @"You have two more chances" rredStyle:UIAlertControllerStyleAlert ];
Uialertaction * action = [uialertaction actionwithtitle: @ "confirm" style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"---------");
}];
[vc addAction:action];
[self presentViewController:vc animated:YES completion:nil];
});
         }
        }
      }];
    }
  }
  else
  {
    Nslog (@ "sorry, the system version is too low");
  }

summary

The above is a brief introduction of fingerprint identification in IOS development introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!