WKWebview loadHTMLString not load local resources (eg. images, css, js)

Hello.


In my app i am trying migrate from UIWebView to WKWebView.


I have downloaded html file in App support dir. This file is fully enrypted, thus i have to use loadHTMLString method for loading it into webview.


Using UIWebView - everything works fine.

Using WKWebView - extrenal files (css, images, js) are not loaded.


Yes, I use correct baseURL.

If i use loadFileURL on non-enrypted file, all external files are loaded.

It works on simulator, not on real device.


It seems that "loadHTMLString" in WKWebView is not working properly - because of app sandbox?? Or am I missing something??


Thank you!

Replies

Hello, I'm having the same problem, any updates about that? How did you fixed that? thanks

Post not yet marked as solved Up vote reply of ozd Down vote reply of ozd

You are replying to a year-old thread. Chances are, you will figure it out in a couple of days too. Then you, too, will ignore a similar reply to your post in Feb. 2021.

Have you find any solution to this?

I fixed problem.

Before when I used UIWebView, I replaced images in the source of page to this structure:

Code Block
let imageFixedString =  "file://" + Bundle.main.path(forResource: imageLink, ofType: nil)
bodyPage.replacingOccurrences(of: imageLink, with: imageFixedString)
webView.loadHTMLString(bodyPage, baseURL: nil)


But when I move to WKWebview, is stoped work.

In my situation, no need replace image address, but need set baseURL:

Code Block
let baseUrlWk = Bundle.main.bundleURL
webView.loadHTMLString(bodyPage, baseURL: baseUrlWk)




This works for me
Code Block
webView.loadHTMLString(htmlContent, baseURL: Bundle.main.resourceURL)

It works in iOS 14.x but not in iOS 12.X. Did you also experience this problem?