TouchID 取消应该关闭视图控制器

TouchID canceling should dismiss the view controller

对于我的应用程序,我需要使用 TouchID 保存一页。因此,用户有点被迫使用 TouchID,或者如果设备不支持它,则使用密码。如果用户取消TouchID认证,我想让View消失,回到根视图。我已经有那个工作了,但不知何故它不再工作了,我真的不知道为什么?!我只是复制到取消选项,其余的我想都无所谓。

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    
}

evaluatePolicy 调用的文档说:

"Reply block that is executed when policy evaluation finishes. This block is evaluated on a private queue internal to the framework in an unspecified threading context."

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

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