swift - touchID takes long time to load

廉价感情. 提交于 2019-12-08 04:14:12

问题


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.

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.

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.

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?

Any help explaining this would be greatly appreciated!


回答1:


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:

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

}



来源:https://stackoverflow.com/questions/28967538/swift-touchid-takes-long-time-to-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!