iOS 指纹识别/面部识别/生物识别

前言

指纹识别,面部识别都属于生物识别

1.配置info.plist

// 跟相机一样需要在info.plist文件中添加配置,否则会闪退

//  字段: NSFaceIDUsageDescription


2.校验方式

LAPolicy 有两种方式

  . LAPolicyDeviceOwnerAuthenticationWithBiometrics iOS8.0以上支持,只有指纹验证功能

    . LAPolicyDeviceOwnerAuthentication iOS 9.0以上支持,包含指纹验证与输入密码的验证方式  (输入密码也可以验证通过)

-(void)click{

        LAContext * context = [[LAContext alloc]init];

          context.localizedFallbackTitle = @"ddd";

          context.localizedCancelTitle = @"ccc";


    NSError * error = nil;

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

        // 支持指纹

        NSLog(@"可以验证");

        [self unlock];


    }else{

        NSLog(@"不能验证");

        [self recoveryLock];


    }

}


-(void)unlock{



    LAContext *    context = [[LAContext alloc]init];

             context.localizedFallbackTitle = @"ddd";

             context.localizedCancelTitle = @"ccc";

    NSString * reason = @"999999 ";


      [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {

                    dispatch_async(dispatch_get_main_queue(), ^{

                if (success) {

                    NSLog(@"验证成功");


                        [self.navigationController pushViewController:[PUSHViewController new]  animated:YES];


                }else{

                    NSLog(@"验证失败");

    //                 NSLog(@"不支持指纹");

                            switch (error.code) {

                                case LAErrorTouchIDNotEnrolled:

                                    NSLog(@"设备没有注册touchID");

                                    break;

                                case LAErrorUserCancel:

                                        NSLog(@"点击了取消按钮");

                                        break;

                                    case LAErrorAuthenticationFailed:

                    //                 LAPolicyDeviceOwnerAuthentication 可以支持输入密码,进行验证

                                            NSLog(@"LAErrorAuthenticationFailed,连续三次输入错误,支付宝弹出指纹不匹配");

                                            break;

                                    case LAErrorPasscodeNotSet:

                    //                 LAPolicyDeviceOwnerAuthentication 可以支持输入密码,但是用户没有设置密码

                                            NSLog(@"用户没有设置密码");

                                            break;

                                    case LAErrorTouchIDNotAvailable:

                                            NSLog(@"不支持指纹,可能手机版本低");

                                            break;

                                case LAErrorUserFallback:

                                        NSLog(@"点击输入密码按钮,,可以处理点击事件");

                                        break;


                                    case LAErrorTouchIDLockout:

                    //  类似支付宝,多次输入错误的话,锁定touchID,用密码接触锁定以后重新,重新支持touchID

                                {


                                    NSLog(@"错误次数超过上限,需要手动输入密码");

                                    [self recoveryLock];

                                }

                                    break;

                                default:

                                    break;

                            }


                }

                         });

            }];

}

-(void)recoveryLock{

    NSLog(@"弹出输入密码");


    LAContext *    context = [[LAContext alloc]init];

             context.localizedFallbackTitle = @"ddd";

             context.localizedCancelTitle = @"ccc";


    [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"22222" reply:^(BOOL success, NSError * _Nullable error) {

        if (success) {

            NSLog(@"输入密码验证成功");

            [self unlock];

        }else{

            NSLog(@"输入密码验证失败");

        }

    }];


}


reason = @"999999 ";localizedCancelTitle=@“6666”:localizedFallbackTitle = @"77777";


指纹输入错误reason = @"999999 ";localizedCancelTitle=@“6666”:localizedFallbackTitle = @"77777";


reason = @"999999 ";localizedCancelTitle=@“”:localizedFallbackTitle = @"";只有一个界面


localizedReason = @“999999”,可以自定义
验证成功,没有再主线程进行操作的话,会有上面的提示

GitHub代码地址

你可能感兴趣的:(iOS 指纹识别/面部识别/生物识别)