【问题标题】:How to wait TouchID until it finishes?如何等待 TouchID 完成?
【发布时间】:2016-04-19 06:09:52
【问题描述】:

我想在我的应用程序中集成 TouchID。基于真实指纹,我将让用户进行身份验证/不进行身份验证。为此,在我的 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方法中的block如果不等待authenticateWithFingerPrint方法返回false,意味着即使用户登录也不会被认证。

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

谢谢:)

【问题讨论】:

    标签: fingerprint touch-id


    【解决方案1】:

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

    您可能应该做的是在您尝试阻止访问的 ViewController 之前插入一个新的 ViewController 并在该屏幕上显示 TouchID 提示。成功通过身份验证后,将您限制访问的 ViewController 推送到堆栈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2011-08-25
      • 2020-11-10
      相关资源
      最近更新 更多