wkwebview iOS 10停止加载文件在重新访问WebView后使用LocalFileurl加载文件



我对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)'我在调试控制台中收到相当奇怪的错误消息:

表面创建的大小失败:(0 0(和格式:(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中起作用。。

我也知道iOS 10.3 Beta中的错误,带有wkwebview问题和带有空格的文件路径 - 但我还使用具有相同结果的文档DIR对此进行了测试。

但是过了一会儿,我得到了此日志输出:

无法发信号com.apple.webkit.networking:113:找不到指定的服务

此答案表明,它可以与不正确添加作为子视图的WebView有关。但是我看不出我在将网络视图添加到子视图中是否做错了...

update

进一步调试视图层次结构是" 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:

多亏了这个答案,我可以解决模棱两可的布局,并且视图层次结构调试器很高兴 - 但不幸的是,这并没有解决WebView的问题 - 它仍然没有响应LoadFileurl请求!

好,最后我找到了解决方案:

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

所以我以某种方式失去了对旧且可见的网络浏览量的引用,因此它不再响应。

使用正确的实例后,一切都起作用。但是我仍然想知道为什么这不会导致其他一些错误指向这一点。