A brief introduction to fingerprint recognition in iOS development. In the iPhone series, it has been a fingerprint recognition function since 5S and is open to fingerprint verification interface when it was iOS8.
Therefore, when we are conducting fingerprint recognition applications, we need to judge the model and system version.
The code is as follows. The following is actually the difference between LAPolicyDeviceOwnerAuthentication and LAPolicyDeviceOwnerAuthenticationWithBiometrics, and the detection system version is through [UIDevice currentDevice]. To determine whether the device is available. Touch ID is determined by the method canEvaluatePolicy: error:. It is also important to note that the following operations that verify whether fingerprint recognition is successful are performed in the child thread by default, so if we want to do UI operations, we must return to the main thread to execute. Available and utilizeddispatch_async(dispatch_queue_t _Nonnull queue, ^{ })
This function is implemented and you can just pass it into the main queue. Also, we can also make some judgments based on the Eror code, see what the user causes the error, and then make corresponding output.
//1. Determine whether the system version is greater than or equal to 8.0. If it is greater than or equal to or equal to, it means that fingerprint recognition can be used. if([UIDevice currentDevice].>=8.0) { //The function of determining whether fingerprint recognition can be used can only be used after 5S //Create the context of the LA object LAContext * context = [[LAContext alloc]init]; //Discern whether the device supports fingerprint recognition //Evaluate means evaluation //Policy means policy // Used to check whether the touchID is available in the current device if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) { //LAPolicyDeviceOwnerAuthentication If we input the fingerprint three times wrong, a password box will pop up. If we do not enter the password. You can also have two chances to verify your fingerprint if both The system password box will continue to pop up for you to enter If you didn't entertouch IDWill be locked,andLAPolicyDeviceOwnerAuthenticationWithBiometricsThe input system will not pop up Password box,After three errors,No processing is done by default,We can also click on fingerprint recognition to enter,But if the input error is still done twicetouch idWill be locked //It means that fingerprint recognition technology can be used [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"Please verify your fingerprint to pay" reply:^ (BOOL success, NSError * _Nullable error) { //It is executed in the child thread, so if you want to update the UI, you must return to the main thread to execute. //Judge whether it is successful if(success) { NSLog(@"%@",[NSThread currentThread]); NSLog(@"Verification Successfully"); } else { NSLog(@"Verification failed"); } NSLog(@"%@",[NSThread currentThread]); NSLog(@"%@",error); if(error) { if(==-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(==-1) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController * vc = [UIAlertController alertControllerWithTitle:@"The fingerprint has been misprinted 3 times" message: @"You have two more chances" preferredStyle: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"); }
Summarize
The above is a brief introduction to fingerprint recognition in iOS development introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!