ios - wkwebview ios 10 在重新创建 webview 后停止使用 localfileurl 加载文件

标签 ios swift3 autolayout wkwebview uiview-hierarchy

我对 wkwebview 和 ios 10.3 以及 swift 3 有一个奇怪的行为。

我正在使用 wkwebview 显示本地 html 文件(在应用程序支持下)。 第一次启动一切正常,本地网站(包括 js 和 css)工作正常。但是,如果我从 Internet 获取内容更新(涉及删除 html 目录和提取新文件),webview 将不会加载文件。如果我只是尝试重用现有的 webview:

self.webView?.loadFileURL(URL(fileURLWithPath: ( sharedConfig.WEBROOT + self.getIndexPage(requestProps.landingPage))), allowingReadAccessTo: sharedConfig.BASEURL)

甚至使用 javascript 调用 window.location.href='\(self.landingPage)' 我在调试控制台中收到相当奇怪的错误消息:

Surface creation failed for size: (0 0) and format: (0)

如果我尝试重新初始化 wkwebview:

` ...

    DispatchQueue.main.async {

    for subview in self.view.subviews {
        if subview is WKWebView {
            subview.removeFromSuperview()
        }
    }

    let contentController = WKUserContentController();
    contentController.add(
        self,
        name: "callbackHandler"
    )
    let processPool: WKProcessPool = WKProcessPool.init()

    let config = WKWebViewConfiguration()
    config.processPool = processPool
    config.userContentController = contentController
     ...

    self.webView = WKWebView(
        frame: CGRect.zero,
        configuration: config
    )

    self.webView!.uiDelegate = self
    self.webView!.navigationDelegate = self

    self.webView!.translatesAutoresizingMaskIntoConstraints = false
    self.webView!.allowsBackForwardNavigationGestures = true

    let localfilePath = sharedConfig.WEBROOT + absPath

    let localFileUrl = URL(fileURLWithPath: localfilePath)


    self.webView?.tag = 12
    self.view.addSubview(self.webView!)
    let height = NSLayoutConstraint(item: self.webView!, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1, constant: 0)
    let width = NSLayoutConstraint(item: self.webView!, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1, constant: 0)
    self.view.addConstraints([height, width])
    _ = self.webView?.loadFileURL(localFileUrl, allowingReadAccessTo: sharedConfig.BASEURL)
    }

没有任何反应 - 显示的 html 没有变化,没有错误,没有卡住 - 本地网站像往常一样工作(除了涉及 loadFileURL 调用的函数 - 但这段代码在 ios 9.3 中工作,我确信它在 ios 10 中工作过一次.

我也知道 ios 10.3 beta 中的一个 bug,它有 wkwebview 问题和带空格的文件路径 - 但我也用 Documents 目录测试了这个,结果相同。

但过了一会儿我得到了这个日志输出:

Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service

这个答案here表示,它可能与未将 webview 正确添加为 subview 有关。但是我看不出我把 webview 添加到 subview 有没有做错...

更新

View 层次结构的进一步调试结果是“wkwebview 的位置不明确”。

po [[UIWindow keyWindow] _autolayoutTrace] 结果如下:

•UIWindow:0x7fbce4b06380 - AMBIGUOUS LAYOUT
|   UITransitionView:0x7fbce2424cc0
|   |   •UIView:0x7fbce2611430
|   |   |   *_UILayoutGuide:0x7fbce2612150
|   |   |   *_UILayoutGuide:0x7fbce260cf10
|   |   |   *WKWebView:0x7fbce508b000'Appname'- AMBIGUOUS LAYOUT for WKWebView:0x7fbce508b000'Appname'.minX{id: 72}, WKWebView:0x7fbce508b000'Appname'.minY{id: 73}
|   |   |   |   WKScrollView:0x7fbce508b600
|   |   |   |   |   WKContentView:0x7fbce5086000
|   |   |   |   |   |   UIView:0x7fbce25194c0
|   |   |   |   |   |   |   UIView:0x7fbce25136e0
|   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261f4f0
|   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce26201c0
|   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620020
|   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261fce0
|   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620500
|   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261fe80
|   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251a4d0
|   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251a330
|   |   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2522790
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   UIScrollView:0x7fbce4069000
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce251e230
|   |   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce261e280
|   |   |   |   |   |   |   |   |   WKCompositingView:0x7fbce2620360
|   |   |   |   |   UIView:0x7fbce2509060
|   |   |   |   |   UIImageView:0x7fbce4b1fb00
|   |   |   |   |   UIImageView:0x7fbce4b1f910

Legend:
    * - is laid out with auto layout
    + - is laid out manually, but is represented in the layout engine because translatesAutoresizingMaskIntoConstraints = YES
    • - layout engine host

但现在我有更多的问题:

  • “模棱两可的布局”是否会导致这种奇怪的 WKWebView 行为?
  • 如果是这样,我该如何解决这种歧义?

更新 2:

感谢this answer我可以解决不明确的布局并且 View 层次结构调试器很高兴 - 但不幸的是这并没有解决 webview 的问题 - 它仍然没有响应 loadFileURL 请求!

最佳答案

好吧,我终于找到了解决方案:

我不小心创建了一个新的 ViewController 对象而不是使用当前实例,我的代码在新的 ViewController 上初始化了另一个 webview。

所以我以某种方式丢失了对旧的和可见的 webview 的引用,因此它不再响应。

在使用正确的实例后,一切正常。但我仍然想知道为什么这没有导致指向此的其他一些错误。

关于ios - wkwebview ios 10 在重新创建 webview 后停止使用 localfileurl 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769752/

相关文章:

ios - rac_signalForSelector : needs empty implementation

ios - 使用 swift 以编程方式填充 UISegmentedControl

ios - Swift 3 通用参数

ios - constraintsWithVisualFormat 无法正常工作?

iphone - iOS Dismis 的 ViewController 堆栈

ios - 定义 FastImageCache FICImageFormats 的最佳实践?

ios - 导致 "Cannot pass immutable as inout"的 Swift 3 转换

ios - iOS 内部 UITableViewCell 中的动态 View

ios - 使 UILabels/UITextFields 在所有手机上按比例看起来相同

ios - 在 UIScrollView 中旋转 UIImageView