为应用添加TouchID/FaceID验证遇到的问题-localizedFallbackTitle自定义事件

最近很懒(其实一直都很懒),长话短说。添加完FaceID/TouchID验证后,更改了LAContext实例的localizedFallbackTitle,正常情况下两次验证失败会弹出下图


为应用添加TouchID/FaceID验证遇到的问题-localizedFallbackTitle自定义事件_第1张图片
IMG_4F568703A00D-1.jpeg

右边按钮默认是密码验证,下面的方法 policy参数传LAPolicyDeviceOwnerAuthentication时,在两次验证失败后点击会弹出密码验证页面,传LAPolicyDeviceOwnerAuthenticationWithBiometrics则不会。

- (void)evaluatePolicy:(LAPolicy)policy
       localizedReason:(NSString *)localizedReason
                 reply:(void(^)(BOOL success, NSError * __nullable error))reply;

上图中右边按钮文字已改,但是点击事件半天没找到,硬着头皮读了一遍文档最后结果是在block判断返回error的code,当code == LAErrorUserFallback时,即可自定义点击事件。


        NSError *error;
        LAContext *context = [LAContext new];
        context.localizedFallbackTitle = @"验证码登录";
        BOOL canAuthentication = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
        if (canAuthentication) {
            [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"验证FaceID" reply:^(BOOL success, NSError * _Nullable error) {
                //注意iOS 11.3之后需要配置Info.plist权限才可以通过Face ID验证
                if (success) {
                    NSLog(@"验证成功");
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [weakFaceIDView removeFromSuperview];
//                        [[NSNotificationCenter defaultCenter] removeObserver:self];
                    });
                } else {
                    LAError errorCode = error.code;
                    if (errorCode == LAErrorUserFallback) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //弹出登录页面
                            LoginViewController *loginVC = [[LoginViewController alloc] init];
                            [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:loginVC animated:YES completion:nil];
                        });
                    } 
                }
            }];
        }
    };

放个Demo https://github.com/onexf/FaceID-TouchIDDemo

你可能感兴趣的:(为应用添加TouchID/FaceID验证遇到的问题-localizedFallbackTitle自定义事件)