TouchID取消应取消视图控制器

时间:2016-02-23 08:33:01

标签: ios swift touch-id

对于我的应用,我需要使用TouchID保存一页。因此,用户有点使用TouchID,或者如果设备不支持,则使用密码。如果用户取消了TouchID身份验证,我想要查看以消失并返回到根视图。我已经有了这个工作,但不知何故它不再起作用了,我真的不知道为什么?!我只是复制到取消选项,其余的并不重要我猜。

func authenticateUser() {

    let context = LAContext()
    var error: NSError?
    let reasonString = "Authentication is needed to access your App"

    if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error){

        context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success, policyError) -> Void in
            if success {
                print("authentification successful")


                }

            }else{

                switch policyError!.code{
                case LAError.SystemCancel.rawValue:
                    print("Authentification was canceled by the system")
                case LAError.UserCancel.rawValue:
                    print("Authentication was canceled by user")
                    self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
                //Yes I also tried popToRootViewController, still doesn't work    
}

1 个答案:

答案 0 :(得分:1)

evaluatePolicy调用的文档说:

“策略评估完成时执行的回复块。在未指定的线程上下文中,在框架内部的私有队列上评估此块。”

所以问题是你试图从错误的线程调用导航。您需要在UI线程上进行该调用。例如:

dispatch_async(dispatch_get_main_queue()) {
     // Navigation goes here
}