0

HTML

This above is my HTML which should be displayed on my webView. Everything displays fine except images. I highlighted an example of img tag.
Does anyone know a reason why? I have made a folder in my app with this path and also on desktop in every case, but it doesn't work.

Code:

var html = """
        <!DOCTYPE html>
        <html>
        <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
    """

func to load html&css:

func loadHtmlCss() {
    guard let path = Bundle.main.path(forResource: "style", ofType: "css") else { return }
    let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
    html += cssString
    html += "</style></head>"

    html += cards[Int(slider.value) - 1].title

    html += "<br><hr>"

    html += cards[Int(slider.value) - 1].content // this is shown on the picture above.
    html += "</body>"
    webView.loadHTMLString(html, baseURL: nil)
}
faris97
  • 402
  • 4
  • 24
  • 1
    See this [answer](https://stackoverflow.com/a/747644/3050006). Notice how baseURL is set while loading the HTML string. Although the code is in Objective-C you should be able to convert it to Swift easily. – Ayazmon Feb 21 '20 at 08:49
  • It is 11 years old objC question, already looked but didnt find an answer – faris97 Feb 21 '20 at 08:51
  • Can you please share your code on how you are loading the contents of your WebView please? – Ayazmon Feb 21 '20 at 08:52
  • I posted. But I dont think it can help very much. Also, it is a bit unclear.. – faris97 Feb 21 '20 at 08:59
  • Ok see that 11 year old ObjC answer was your solution. You are loading the webView with `baseURL` param being `nil`. You need to set it by `webView.loadHTMLString(html, baseURL: URL(fileURLWithPath: Bundle.main.bundlePath))`. Also your code is fine, could be better but fine :) – Ayazmon Feb 21 '20 at 08:59
  • Ok I changed it but now instead of images I have question marks(?) with blue background. – faris97 Feb 21 '20 at 09:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208234/discussion-between-ayazmon-and-faris97). – Ayazmon Feb 21 '20 at 09:02

0 Answers0