如何等待TouchID完成?

时间:2016-04-19 06:09:53

标签: fingerprint touch-id

我想在我的应用程序中集成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方法中的块不等待authenticateWithFingerPrint方法并返回false,则意味着即使用户登录,也不会对其进行身份验证。

那么,如果阻止等待authenticateWithFingerPrint方法怎么办?

谢谢:)

1 个答案:

答案 0 :(得分:1)

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

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

相关问题