WKWebView fails to load images and CSS using loadHTMLString(_, baseURL:)

前端 未结 10 820
抹茶落季
抹茶落季 2020-12-14 14:05

Apple\'s recommendation:

In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.

Thus, I have r

相关标签:
10条回答
  • 2020-12-14 14:57

    I've been experimenting with this as well, with similar restrictions, and the problem appears to be that paths aren't resolved unless baseURL references the application bundle. It doesn't work if you, for example, have something in the application's documents.

    Edit: I have filed a radar for this rdar://29130863

    0 讨论(0)
  • 2020-12-14 14:58

    I had this problem today, I've found the solution and potentially the cause:

    loadHTMLString(String, baseURL: URL?) 
    

    This function doesn't allow the rendered HTML to access local media, as far as I'm aware, this is because it would be an injection risk, this could allow rendered HTML to access and manipulate your local file system. With a html string, that could come from anywhere or anyone.

    loadFileURL(URL, allowingReadAccessTo: URL)
    

    With this function, you point the WKWebview to the html file in your FileManager, and to the containing folder with 'allowingReadAccessTo'. Because the html is stored within the FileManager, it will allow the rendered HTML to access locally stored media.

    If you don't have the html file stored locally for some reason(I assume you do), You could write the html sting into a .html file, then point to the URL of that file. However, this is just subverting Apple's protection, so do it at your own peril (don't do it).

    This is just the solution that worked for me and my understanding of why we're having the problem to begin with.

    Edit #1: Typo.

    Edit #2: I've since found another nuance, When stating the 'allowingReadAccessTo:' URL, if the HTML itself needs to access things in parent folders (ie: .css, .js files), you need to specify the parent folder, not necessarily the location of the HTML itself, this will then implicitly allow access to the child folders as required also. For me, this problem was only apparent on a physical device, this didn't seem to have an effect whilst running in simulator, likely another discrepancy between how permissions work on simulator and a physical device.

    0 讨论(0)
  • 2020-12-14 14:58

    When I used UIWebview, I used baseURL as,

    let baseUrl = NSURL(string: Bundle.main.path(forResource: "cms", ofType: "html")!)! as URL
    
    webView.loadHTMLString(bodyPage, baseURL: baseUrl)
    

    But for the WKWebView, I used baseURL as

    let baseUrl = Bundle.main.bundleURL
    
    webView.loadHTMLString(bodyPage, baseURL: baseUrl)
    

    This works for me.

    0 讨论(0)
  • 2020-12-14 14:59

    Personally, I had to switch to using XWebView as the out-of-the-box behavior of WKWebView does not allow loading of local files. XWebView tricks it by loading up a local web server in the background and directing local traffic thru it. (XWebView is based on top of WKWebView)

    Seems a bit overkill, but that is what I ended up having to do.

    0 讨论(0)
提交回复
热议问题