首页 文章

当WKWebView尝试呈现WKActionSheet时,长按会导致错误/警告

提问于
浏览
8

我的视图层次结构如下所示:

  • (root)PRSBaseViewController - 一个UIViewController子类 - 具有子viewControllers

  • (已提交)PRSModalWebViewController - UINavigationController子类

  • (推,动画:否)PRSWebViewController - 一个UIViewController子类--WKWebView是一个子视图 .

当我尝试长按WebView中的链接时,我收到错误:

Warning: Attempt to present <WKActionSheet: 0x127520d10> on <PRSBaseViewController: 0x1275117f0> whose view is not in the window hierarchy!

而不是使用 presentViewController:animated:completion 呈现导航,而是使用 addChildViewController: 舞蹈将视图控制器添加到层次结构中 . 我没有错,这很奇怪 .

有谁知道什么可能导致视图层次结构问题?

Update: 我做了Gist of all my classes

2 回答

  • 4

    我遇到过类似的行为 - 并且还没有弄清楚这是怎么发生的 .

    这是我的解决方法 . 由于WKWebView正在调用我的RootViewController,我通过覆盖RootViewController的presentViewController处理它:animated:completion: - 如果RootViewController已经有一个presentViewController,那么它将消息转发给该控制器 . 它似乎解决了警告信息,并在模态视图中使用WKWebView时给了我长按菜单 .

    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    
        // Unsure why WKWebView calls this controller - instead of it's own parent controller
        if (self.presentedViewController) {
            [self.presentedViewController presentViewController:viewControllerToPresent animated:flag completion:completion];
        } else {
            [super presentViewController:viewControllerToPresent animated:flag completion:completion];
        }
    }
    

    或者在快速:

    override func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) {
        if presentedViewController != nil {
            // Unsure why WKWebView calls this controller - instead of it's own parent controller
            presentedViewController?.presentViewController(viewControllerToPresent, animated: flag, completion: completion)
        } else {
            super.presentViewController(viewControllerToPresent, animated: flag, completion: completion)
        }
    }
    
  • 13

    我们也在PSPDFKit中看到了这个问题,在调查了UIKit组件和 WKWebView 来源之后,我们发现了一个仍然很糟糕但不具有侵入性的解决方法 .

    主要策略是选择性并及时应用解决方法 - 然后再次清理 . 你可以在这里阅读源代码:

    https://gist.github.com/steipete/b00fc02aa9f1c66c11d0f996b1ba1265

    请尽情享受rdar://26295020所以这有望在iOS 10中及时修复 . (该漏洞自iOS 8开始存在,并首次在iOS 8b5上报告 . )

相关问题