WKWebView failing in 17.5 beta

hi I have been using WKWebView embedded in a UIViewRepresentable for displaying inside a SwiftUI View hierarchy, but when I try the same code on 17,5 beta (simulator) the code fails.

In fact, the code runs (no exceptions raised or anything) but the web view does not render. In the console logs I see:

Warning: -[BETextInput attributedMarkedText] is unimplemented

Error launching process, description 'The operation couldn’t be completed. (OSStatus error -10814.)', reason ''

The code I am using to present the view is:

struct MyWebView: UIViewRepresentable {
    let content: String

    func makeUIView(context: Context) -> WKWebView {
        // Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into <head>
        let source: String = """
        var meta = document.createElement('meta');
        meta.name = 'viewport';
        meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
        var style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = '*:focus{outline:none}body{margin:0;padding:0}';
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(meta);
        head.appendChild(style);
"""
        
        let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

        let userContentController: WKUserContentController = WKUserContentController()
        let conf = WKWebViewConfiguration()
        conf.userContentController = userContentController
        
        userContentController.addUserScript(script)
        
        let webView = WKWebView(frame: CGRect.zero /*CGRect(x: 0, y: 0, width: 1000, height: 1000)*/, configuration: conf)
        webView.isOpaque = false
        webView.backgroundColor = UIColor.clear
        webView.scrollView.backgroundColor = UIColor.clear
        webView.scrollView.isScrollEnabled = false
        webView.scrollView.isMultipleTouchEnabled = false
        if #available(iOS 16.4, *) {
            webView.isInspectable = true
        }
        return webView
    }
    
    func updateUIView(_ webView: WKWebView, context: Context) {
        webView.loadHTMLString(content, baseURL: nil)
    }
}

This has been working for ages and ages (back to at least ios 15) - something changed. Maybe it is just a problem with the beta 17.5 release?

  • Are you still hitting this on beta 3? And if so it's probably worth filing a Feedback report.

  • Not sure what you mean by 'Beta 3' - do you mean iOS 17.5 Beta 3? Currently my Xcode 15.4 Beta only has ios 17.5 beta 2 so I am not able to verify if beta 3 fixes this untl this is available from Apple.

Add a Comment

Replies

I'm having the same problem as well, actually, I noticed that the webview is having issues to render on Xcode cloud since xcode 15 release.

For me this seems limited to the iOS version - and for now just on a simulator (not been able to try on a real device yet... until 17.5 is released officially). I am using Xcode 15.4 Beta to build the App, but it works just fine in ios 17.4 or earlier.

I also tried on VisionOS 1.2 (beta) and this works fine. For completeness I also tried iPad (17.5 beta) but that has the same issue iPhone 17.5 has. So it seems a generic issue with ios on iphone and ipad, but it is not an issue with the latest visionOS release.

For completeness, I created the simplest app possible that illustrates this:

//
//  ContentView.swift
//  TestWKWebView
//
//

import SwiftUI
import WebKit

struct ContentView: View {
    var body: some View {
        SVGWebView()
    }
}

struct SVGWebView: UIViewRepresentable {
    let svg: String = "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /><title>Hello ...</title></head><body><p>Greetings ...</p></body></html>"

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView(frame: CGRect.zero)
    }
    
    func updateUIView(_ webView: WKWebView, context: Context) {
        webView.loadHTMLString(svg, baseURL: nil)
    }
}

#Preview {
    ContentView()
}

This fails too, as I suspected, but worth doing anyway. It works fine on ios 17.5 or earlier.