1

Currently, I'm using WKWebView to do loadHTMLString(_ string: String, baseURL: URL?) and for some reasons, it wont load the content page from the source. However, If I switch to UIWebView and do the same thing and surprisedly, content page can be displayed successfully. Below are my codes:

This is WKWebView:

 if let html = try? String(contentsOfFile: page.href, encoding: String.Encoding.utf8) {
    wkWebview.loadHTMLString(html, baseURL: URL(string:book.resourceDir! + "/"))
 }

This is UIWebView:

 if let html = try? String(contentsOfFile: page.href, encoding: String.Encoding.utf8) {
   webView.loadHTMLString(html, baseURL: URL(string:book.resourceDir! + "/"))
}

Notice: the html and baseURLare identical for both uiwebview and wkwebview.

Have anyone had this problem and have any ideas about it. All comments are welcomed.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
tonytran
  • 1,068
  • 2
  • 14
  • 24

1 Answers1

0

I want to provide you my solution:

wkWebview.loadHTMLString("<html><body>\(put_here)</body></html>", baseURL: nil)

Just paste your variable that contains html code instead "put_here". If you string contains html tags like "html", "body" then remove them from loadHTMLString()

Nosov Pavel
  • 1,571
  • 1
  • 18
  • 34