iOS 18 HTML images are not loading in WKWebView

iOS 18 WKWebView images are not loading, no errors in older versions.

The example while loading HTML is as follows. A problem occurs when you pull an image from a url. To get a URL image you need a cookie or something. I add them too, but it doesn't work on iOS 18. I get the error "An error occurred trying to load the resource." and a blue question mark on the screen.

 webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) 


  <html>
                <head>
                <style>
                body {
                    font-family: -apple-system;
                    margin: 0;
                    padding: 0;
                    text-align: center;
                }
                img {
                    width: 100%;
                    height: auto;
                }
                </style>
                </head>
                <body>
                    <h1>Resim Görüntüleme</h1>
                    <img src="https://xx.***.xx/example.png">
                </body>
                </html>
                """
Answered by ahmetozkanio in 806229022

Yes, I have identified the issue. Normally, when loading this image URL with URLRequest, there is no problem, and cookies are set when loaded in the WebView. However, my problem occurs here:


webView.loadHTMLString(htmlString, baseURL: baseURL)

If you leave the baseURL part as nil, the cookies are not recognized, and the images could not be loaded in iOS 18. If you set your default domain URL in the baseURL part, the issue is resolved because the WebView sets the cookies to the baseURL and loads the HTML from there. Thank you.

Accepted Answer

Yes, I have identified the issue. Normally, when loading this image URL with URLRequest, there is no problem, and cookies are set when loaded in the WebView. However, my problem occurs here:


webView.loadHTMLString(htmlString, baseURL: baseURL)

If you leave the baseURL part as nil, the cookies are not recognized, and the images could not be loaded in iOS 18. If you set your default domain URL in the baseURL part, the issue is resolved because the WebView sets the cookies to the baseURL and loads the HTML from there. Thank you.

Hi,

You can use an html string to load, you just need to use a diffferent mechanism for handling the load.

Please see WKURLSchemeHandler which can also load local resources.

For example:

class LocalLoaderCustomSchemeHandler: NSObject, WKURLSchemeHandler {
    
    func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
        guard let url = urlSchemeTask.request.url else {
            return
        }
//... a few lines later:
} else if type == LocalObjectType.css.rawValue {
            if let resource = Bundle.main.url(forResource: "style", withExtension: "css"),
               let resourceData = try? Data(contentsOf: resource) {
                data = resourceData
                response = URLResponse(
                    url: url,
                    mimeType: "text/css",
                    expectedContentLength: data!.count,
                    textEncodingName: nil)
            }
        } else if type == LocalObjectType.javascript.rawValue {
            if let resource = Bundle.main.url(forResource: "script", withExtension: "javascript"),
               let resourceData = try? Data(contentsOf: resource) {
                data = resourceData
                response = URLResponse(
                    url: url,
                    mimeType: "text/javascript",
                    expectedContentLength: data!.count,
                    textEncodingName: nil)
            }
        } else if type == LocalObjectType.png.rawValue {
            if let resource = Bundle.main.url(forResource: "image", withExtension: "png"),
               let resourceData = try? Data(contentsOf: resource) {
                data = resourceData
                response = URLResponse(
                    url: url,
                    mimeType: "image/png",
                    expectedContentLength: data!.count,
                    textEncodingName: nil)
            }
        }

Hopefully this helps.

Rico

WWDR - DTS - Software Engineer

iOS 18 HTML images are not loading in WKWebView
 
 
Q