我发现了一个代码,可以用iPad的swift playground读取html文件。但没有出现在应用程序预览,我得到了错误消息:无法将类型“URL”的值转换为expertes参数类型“String”。我不知道问题出在哪里,你能帮帮我吗?这是我的代码
import SwiftUI
import WebKit
struct ContentView: View {
var body: some View {
VStack {
WebView()
}
}
}
struct WebView:UIViewRepresentable{
func makeUIView(context: Context) -> some UIView {
let webView = WKWebView()
let htmlPath = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www")
let htmlUrl = URL(fileURLWithPath: htmlPath)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl.deletingLastPathComponent())
return webView
}
}
需要帮助来读取iPad版Swift Playgrounds的html文件
1条答案
按热度按时间ajsxfq5m1#
将此行
let htmlPath = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www")
替换为此guard let htmlPath = Bundle.main.path(forResource: "index", ofType: "html") else { return webView }
。或者如果你想的话你可以换成这个。