WKView 内容不显示在 NSViewRepresentable 中 [英] WKView content doesn't display in NSViewRepresentable

查看:25
本文介绍了WKView 内容不显示在 NSViewRepresentable 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 WKWebView 放入 NSViewRepresentable 时遇到问题,然后我的应用程序显示黑屏而不是加载的网页.

I ran into an issue when I put WKWebView in NSViewRepresentable then my app shows a black screen instead of the loaded web page.

import SwiftUI
import WebKit

struct TestView : NSViewRepresentable {
    func makeNSView(context: Context) -> WKWebView  {
        let view = WKWebView()
        if let url = URL(string: "https://www.google.com/") {
            view.load(URLRequest(url: url))
        }
        return view
    }

    func updateNSView(_ view: WKWebView, context: Context) {
    }

}


struct ContentView: View {
    var body: some View {
        TestView()
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

在 macOS 上不要监督的一件事是在 App Sandbox 选项中打开传出连接".

One thing not to oversee on macOS is to turn on "Outgoing Connections" in App Sandbox options.

当然,允许任意加载"也可以是是".

Of course "Allow Arbitrary Loads" may be on yes as well.

如果您想更进一步,您应该考虑实现 LuLuGaGa 中提到的协调器.

If you want to go one step further you should consider to implement a Coordinator as mentioned from LuLuGaGa.

这篇关于WKView 内容不显示在 NSViewRepresentable 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆