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

MLy*_*yck 1 ios touch-id swift xcode6

我正在努力将touchID集成到我的应用程序中.这个过程非常简单,但即使只是使用我的虚拟数据,它在执行任务之前需要大约5秒的时间来验证我的指纹.

这是我的代码:

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")
            }
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

Gar*_*hes 7

文件说明:

此方法异步评估身份验证策略.

您正在一个非主要线程上运行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")
        }
    })
}
Run Code Online (Sandbox Code Playgroud)

}