使用 SwiftUI 读取 HTML 字符串

Fra*_*mbo 5 html swift swiftui

我正在尝试使用 SwiftUI 读取 HTML 格式的内容,我尝试使用几种不同的方法来加载 HTML 字符串,但该字符串没有显示。我正在使用 XCode 11.2.1,非常感谢您的帮助。

import SwiftUI
import Combine
import WebKit

struct SubChapterDetails: View {
    let sentDescription: String
    let webView = WKWebView()

    var body: some View {
           VStack {
               Text("Testing HTML Content")
               Spacer()
               HTMLStringView(htmlContent: "<h1>This is HTML String</h1>")  //It doesn't displays this when the code is executed
               Spacer()
           }
       }
}

struct SubChapterDetails_Previews: PreviewProvider {
    static var previews: some View {
        SubChapterDetails(sentDescription: "")
    }
}

struct HTMLStringView: UIViewRepresentable {
    let htmlContent: String

    func makeUIView(context: Context) -> WKWebView {
        return WKWebView()
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.loadHTMLString(htmlContent, baseURL: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

app*_*sch 7

当 macOS 目标应用程序遇到同样的问题时,我也偶然发现了这个问题。就在我准备脱掉头发的几秒钟前,我在研究时发现了有关权利的提及。

\n

因此,我去激活了Outgoing Connections (Client)权利 \xe2\x80\x93 et voil\xc3\xa0:它有效。

\n

我很惊讶这是必要的,因为我想要加载的 html 是纯粹本地的,即使在我现在概念验证的代码中也是如此。

\n

在此输入图像描述

\n

这是代码:

\n
struct ContentView: View {    \n\n    var body: some View {\n        WebViewWrapper(html: "<h1>Hello World!</h1>")\n    }\n}\n\nstruct WebViewWrapper: NSViewRepresentable {\n    \n    let html: String\n    \n    func makeNSView(context: Context) -> WKWebView {\n        return WKWebView()\n    }\n    \n    func updateNSView(_ nsView: WKWebView, context: Context) {\n        nsView.loadHTMLString(html, baseURL: nil)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n