swift – touchID需要很长时间才能加载

我正在努力将touchID集成到我的应用程序中。 这个过程相当简单,但即使只是使用我的虚拟数据,在执行它的任务之前,它需要大约5秒后validation我的指纹。

这是我的代码:

func requestFingerprintAuthentication() { let context = LAContext() var authError: NSError? let authenticationReason: String = "Login" if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) { context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: authenticationReason, reply: { (success: Bool, error: NSError?) -> Void in if success { println("successfull signin with touchID") self.emailInputField.text = "john.doe@gmail.com" self.passwordInputField.text = "password" self.signIn(self.signInButton) } else { println("Unable to Authenticate touchID") } }) } } 

即使使用虚拟数据,也需要花费太长时间。

当我正常登录时,通过在我的输入字段中键入电子邮件和密码,signIn()函数立即运行。

看看它是否有问题。 我尝试用2行替换它,只需将我带到正确的viewController。 但在对我的指纹进行身份validation后仍需要几秒钟。

我知道这不是手机,也不是touchID。 因为它立即运行我的println(“使用touchID成功登录”)。 之后是什么,由于某种原因它需要几秒钟才能运行?

任何帮助解释这一点将不胜感激!

文件说明:

此方法异步评估身份validation策略。

您正在运行不是主要线程的UI代码。 包装您的代码以使其在主线程上执行:

 func requestFingerprintAuthentication() { let context = LAContext() var authError: NSError? let authenticationReason: String = "Login" if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) { context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: authenticationReason, reply: { (success: Bool, error: NSError?) -> Void in if success { NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in println("successfull signin with touchID") self.emailInputField.text = "john.doe@gmail.com" self.passwordInputField.text = "password" self.signIn(self.signInButton) }) } else { println("Unable to Authenticate touchID") } }) } 

}