Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView in SwiftUI not loading HTML string on macOS

first question on StackOverflow (and relatively new to native app dev targeting macOS).

I'm currently trying to build a simple SwiftUI view, that leverages WKWebView's loadHTMLString function, to display hardcoded HTML string on the screen.

AFAIK webkit does not support SwiftUI as of the moment, so I need to embed AppKit UI in my SwiftUI app using NSViewRepresentable. This what I got so far following the docs and XCode's autocomplete:

import SwiftUI
import WebKit

struct HTMLView: NSViewRepresentable {
    typealias NSViewType = WKWebView
    
    let html = "<h1>Hello wordl</h1>"

    func makeNSView(context: Context) -> WKWebView {
        let webview = WKWebView()
        return webview
    }
    
    func updateNSView(_ nsView: WKWebView, context: Context) {
        nsView.loadHTMLString(html, baseURL: nil)
    }
}

struct HTMLView_Previews: PreviewProvider {
    static var previews: some View {
        HTMLView()
    }
}

Should be noted that preview canvas does not load the HTML (displays empty window).

I then replace the default Text() view in ContentView.Swift with HTMLView(), and run my application.

The application compiles, but the WebView fails to load the HTML (I get an empty window). I get the following errors in console:

WebPageProxy::processDidTerminate: (pid 0), reason 3
WebPageProxy::dispatchProcessDidTerminate: reason = 3
WebPageProxy::processDidTerminate: (pid 0), reason 3
WebPageProxy::dispatchProcessDidTerminate: reason = 3
WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts

Any help with the above would be highly appreciated!

like image 738
MQumairi Avatar asked Feb 01 '21 17:02

MQumairi


People also ask

How do I import HTML into WKWebView?

How to load a HTML string into a WKWebView or UIWebView: loadHTMLString() If you want to load resources from a particular place, such as JavaScript and CSS files, you can set the baseURL parameter to any URL .

What is UIWebView WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


1 Answers

On macOS, although it doesn't seem like this should be necessary, you need to set "Outgoing Connections (Client)" to true in your "Signing and Capabilities" on your target in order for WKWebView to load, even though you're loading from a string and not from an external page.

As soon as I changed this, your example worked fine.

Network settings

like image 141
jnpdx Avatar answered Nov 23 '22 18:11

jnpdx