【问题标题】:Touch ID API responsive is very slowTouch ID API 响应很慢
【发布时间】:2014-12-12 05:02:54
【问题描述】:

我已遵循指南以及 Apple 文档中的 Touch ID API 示例。我在我的应用程序中使用了这个例子。我可以使用 Touch ID 登录。但问题是它的响应速度非常非常慢。将手指放在 Touch ID 上后,我必须等待至少 10 秒才能验证成功/失败。我已经使用了应用程序委托文件中的代码。我还测试了不同的应用程序,但结果是相同的“延迟响应”。在这种情况下,请大家帮帮我。

【问题讨论】:

  • 我应该在调度队列中写代码吗?
  • 我也注意到了这一点。我遵循了 Apple 的示例,在收到回调之前需要相当多的时间。希望有人能给出一些答案,因为 Apple 在这方面的文档乏善可陈。
  • 是的。但我不确定 1password 是如何完美地做到这一点的。
  • 哎呀,真不敢相信我没想到这一点。 Vishal 提供的答案是正确的。我只是认为这是苹果代码的复制和粘贴,但我们需要在主线程上使用 UI 完成所有操作,他/她在成功块上所做的。

标签: ios objective-c authentication touch-id


【解决方案1】:
LAContext *myContext = [[LAContext alloc] init];

NSError *authError = nil;

NSString *myLocalizedReasonString = <#String explaining why app needs authentication#>;

if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {

    [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
              localizedReason:myLocalizedReasonString
                        reply:^(BOOL success, NSError *error) { 

                            if (success) {
                                   // User authenticated successfully, take appropriate action
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                          // write all your code here
                               });
                            } else {
                                   // User did not authenticate successfully, look at error and take appropriate action

                               switch (error.code) {
                                   case LAErrorAuthenticationFailed:
                                       NSLog(@"Authentication Failed");
                                       break;

                                   case LAErrorUserCancel:
                                       NSLog(@"User pressed Cancel button");
                                       break;

                                   case LAErrorUserFallback:
                                       NSLog(@"User pressed \"Enter Password\"");
                                       break;

                                   default:
                                       NSLog(@"Touch ID is not configured");
                                       break;
                               }

                               NSLog(@"Authentication Fails");
                            }
                        }];
} else {
    // Could not evaluate policy; look at authError and present an appropriate message to user
}

【讨论】:

  • 谁能解释一下线程在块执行后做了什么?
【解决方案2】:

你必须在主线程中显示警报视图

dispatch_async(dispatch_get_main_queue(), ^{ 
//update ui
});

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

NSError *error = nil;

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

NSError *error = nil;
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
  [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
          localizedReason:@"Please verify that you are the device owner in order to place the order"
                    reply:^(BOOL success, NSError *error) {
                      dispatch_async(dispatch_get_main_queue(), ^{
                        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) {
                          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                                                          message:@"You are the device owner!"
                                                                         delegate:nil
                                                                cancelButtonTitle:@"Ok"
                                                                otherButtonTitles:nil];
                          [alert show];

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

} 否则 {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                message:@"Your device cannot authenticate using TouchID."
                                               delegate:nil
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil];
[alert show];

}

【讨论】:

    【解决方案3】:

    正如其他人所说,你必须在主线程上做 UI 的事情,对于 Swift 3.0 它是:

    myContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { (success, evaluateError) in
                DispatchQueue.main.async {
                        if (success) {
                            //success
                        } else {
                            //failure
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多