我是 尝试使用 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)
}
}
最佳答案
虽然针对 macOS 的应用程序遇到了同样的问题,但我也偶然发现了这个问题。就在我准备脱下头发的几秒钟前,我在研究时发现了一项权利。
所以,我去激活了Outgoing Connections (Client)
权利——等等:它有效。
我很惊讶这是必要的,因为我想要加载的 html 纯粹是本地的,即使在我现在的概念证明中的代码中也是如此。
这是代码:
struct ContentView: View {
var body: some View {
WebViewWrapper(html: "<h1>Hello World!</h1>")
}
}
struct WebViewWrapper: NSViewRepresentable {
let html: String
func makeNSView(context: Context) -> WKWebView {
return WKWebView()
}
func updateNSView(_ nsView: WKWebView, context: Context) {
nsView.loadHTMLString(html, baseURL: nil)
}
}
关于html - 使用 SwiftUI 读取 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60683077/