WKWebView fails to load images and CSS using loadHTMLString(_, baseURL:)
<p><a href="https://developer.apple.com/reference/uikit/uiwebview#overview" rel="noreferrer">Apple's recommendation</a>:</p>

<blockquote>
  <p>In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.</p>
</blockquote>

<p>Thus, I have replaced my good old <code>UIWebView</code> with a shiny new <code>WKWebView</code>. But what I thought to be an easy exercise (simply swapping the classes and replacing the delegate methods) turned out to be a real mess.</p>

<h1>The Problem</h1>

<p>When I load an HTML string using</p>

<pre><code>loadHTMLString(String, baseURL: URL?)
</code></pre>

<p>the web view loads and renders the pure HTML but <strong>it doesn't load any images or CSS files referenced inside the <code>htmlString</code></strong>.</p>

<p>This happens <strong>only on a real device</strong>!<br />
In Simultor all referenced resources are loaded correctly.</p>

<p><a href="https://i.stack.imgur.com/2f84l.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2f84l.png" alt="Simulator"></a>
<a href="https://i.stack.imgur.com/bDCwA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bDCwA.png" alt="Real Device"></a></p>

<h3><em>Example</em></h3>

<p>I have defined a simple <code>htmlString</code> in my view controller class:</p>

<pre><code>let imageName = "image.png"

let libraryURL: URL // The default Library URL

var htmlString: String {
    return "&lt;html&gt; ... &lt;img src=\"\(imageName)\" /&gt; ... &lt;/html&gt;"
    // "..." represents more valid HTML code incl. header and body tags
}
</code></pre>

<p>The image is stored in the root Library folder so its URL is:</p>

<pre><code>let imageURL = libraryURL.appendingPathComponent(imageName)
</code></pre>

<p>Now I load the <code>htmlString</code> into the web view:</p>

<pre><code>webView.loadHTMLString(htmlString, baseURL: libraryURL)
</code></pre>

<p>and it doesn't load the image even though the <code>baseURL</code> is set correctly.</p>

<h1>Ideas for a Solution</h1>

<ol>
<li><p>Maybe <code>WKWebView</code> has a problem with resolving relative paths so my first idea was to use <strong>absolute paths</strong> inside the HTML string instead.<br />
→ ❌ Doesn't work.</p></li>
<li><p>Two answers to <a href="https://stackoverflow.com/questions/24882834/wkwebview-not-loading-local-files-under-ios-8">another SO post</a> suggested that using<br />
<code>loadFileURL(URL, allowingReadAccessTo: URL)</code><br />
instead of <code>loadHTMLString(...)</code> works in iOS 9+.<br />
→ ✅ That works.</p></li>
</ol>

<p>However, I cannot use solution 2 because my HTML files are encrypted and the decrypted files must not be stored on the disk.</p>

<h1>Question</h1>

<p>Is there any way to load local resources like images and styles using the <code>WKWebView</code>'s</p>

<pre><code>loadHTMLString(String, baseURL: URL?)
</code></pre>

<p>function? Or is still a bug in iOS 9+?</p>

<p><em>(I just cannot believe that Apple provides and recommends using a web view that cannot load any local web content from inside an HTML string?!)</em></p>