9

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!

1
  • Hi Andrew. Just tried now, getting same issues unfortunately. Commented Feb 1, 2021 at 17:50

1 Answer 1

37

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! That was it.
This setting is in "App Sandbox" capability of "Signing & Capabilities", for your information.
You could laugh, but first time this helped me, and then the page still doesn't load even with these both ticks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.