data cosmos
data cosmos

Reputation: 313

wkwebview ios 10 stops loading files with localfileurl after recreation of webview

I have a strange behaviour with wkwebview and ios 10.3 and swift 3.

I am using a wkwebview to display local html files (under Application Support). At first startup everything works and the local website (including js and css) works fine. But if I fetch an update of the content from the internet (which involves deletion of the html directory and extracting new files) the webview won't load the files. If i just try to reuse the existing webview with:

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

or even with a javascript call window.location.href='\(self.landingPage)' I get the rather strange error message in the debug console:

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

If I try to reinitialise the wkwebview with:

` ...

    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)
    }

nothing happens - no change of the displayed html, no errors, no freeze - the local website works as usual (except functions involving a loadFileURL call - but this code works in ios 9.3 and I am sure it worked once in ios 10.

I also know about a bug in ios 10.3 beta with wkwebview problems and file paths with spaces - but I also tested this with the Documents dir with the same result.

But after a while I got this log output:

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

This answer here indicates, that it could relate to not adding the webview correctly as a subview. But I can't see if I did something wrong with adding the webview to the subview...

Update

Further debugging of the view hierarchy turns out an "ambiguous position for wkwebview".

And a po [[UIWindow keyWindow] _autolayoutTrace] results into this:

•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

But now I have even more questions:

Update 2:

Thanks to this answer I could resolve the ambiguous layout and the view hierarchy debugger is happy - but unfortunately this did not resolve the problem with the webview - it still does not respond to the loadFileURL request!

Upvotes: 2

Views: 867

Answers (1)

data cosmos
data cosmos

Reputation: 313

OK, finally I have found the solution:

I accidentally created a new ViewController Object instead of using the current instance and my code initialized another webview on the new ViewController.

So I lost somehow the reference to the old and visible webview and therefore it did not respond anymore.

After using the correct instance everything worked. But I still wonder why this did not result in some other errors pointing to this.

Upvotes: 0

Related Questions