如何等待 TouchID 完成?

How to wait TouchID until it finishes?

我想在我的应用程序中集成 TouchID。根据真实指纹,我会让用户 authenticate/not 进行身份验证。为此,在我的 ViewController 的 viewWillAppear 中,我编写了以下代码:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if ([AppConfig getSettingWithKey:APP_TOKEN_KEY] != nil ){

    if ([AppConfig getIsTouchIDPreferred] == PREFERRED){

        BOOL shouldAuthenticate: [MyTouchIDClass authenticateWithFingerPrint];
        if (shouldAuthenticate == YES){
            //normal flow
        }

    }
}

}

这里,authenticateWithFingerPrint 完成主要工作及其代码:

LAContext *context = [[LAContext alloc] init];
NSError *error = nil;

__block BOOL toBeReturned = NO;
if([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]){
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
            localizedReason:@"Are you the device owner?"
                      reply:^(BOOL success, NSError *error) {
                          if(error){
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                              message:@"There was a problem verifying your identity."
                                                                             delegate:nil
                                                                    cancelButtonTitle:@"Ok"
                                                                    otherButtonTitles:nil];
                              [alert show];

                              return;
                          }
                          if(success){
                              toBeReturned = YES;
                              return;


                          } else {
                              UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                              message:@"You are not the device owner."
                                                                             delegate:nil
                                                                    cancelButtonTitle:@"Ok"
                                                                    otherButtonTitles:nil];
                              [alert show];
                              return;
                          }
                      }];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Your device cannot authenticate using TouchID."
                                                   delegate:nil
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil];
    [alert show];
}

return toBeReturned;

但是,问题是如果 viewWillAppear 方法中的块不等待 authenticateWithFingerPrint 方法和 returns false,这意味着即使用户登录,她也不会被验证。

那么,如何让 if 块等待 authenticateWithFingerPrint 方法?

谢谢:)

您不应在等待 TouchID 时尝试阻塞主线程。

您可能应该做的是在您试图阻止访问并在该屏幕上显示 TouchID 提示的那个之前插入一个新的 ViewController。成功验证后,将您限制访问的 ViewController 压入堆栈。