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

时间:2022-10-19 18:55:52

I'm working on integrating touchID into my application. The process was fairly simple, but even just using my dummy data, it takes about 5 seconds after it authenticated my fingerprint, before it performs it's task.

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

Here's my code:

这是我的代码:

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")
            }
        })
    }
}

even with the dummy data, it takes waaay too long.

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

When I login normally, by typing the email and the password into my inputfields, the signIn() function runs instantly.

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

To see if it was a problem with that. I tried replacing that, with 2 lines that simply takes me to the correct viewController. But it still takes several seconds after it's authenticated my fingerprint.

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

I know it's not the phone, nor touchID. Cause it runs my println("successfull signin with touchID") immediately. It's what comes after that, that for some reason takes several seconds for it to run?

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

Any help explaining this would be greatly appreciated!

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

1 个解决方案

#1


7  

The documentation states:

文件说明:

This method asynchronously evaluates an authentication policy.

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

You are running UI code on a thread that is not the main. Wrap your code to get it to perform on the main thread:

您正在运行不是主要线程的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")
        }
    })
}

}

#1


7  

The documentation states:

文件说明:

This method asynchronously evaluates an authentication policy.

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

You are running UI code on a thread that is not the main. Wrap your code to get it to perform on the main thread:

您正在运行不是主要线程的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")
        }
    })
}

}