如何使用最新版本的Swiftui使用WKWebView?
以前,我使用以下代码在Swiftui的视图中呈现HTML
import WebKit
struct HTMLView: UIViewRepresentable {
let htmlString:String
func makeUIView(context:Context)->WKWebView{
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlString, baseURL: nil)
uiView.isOpaque = false
}
}
struct HTMLView: UIViewRepresentable {
let htmlString:String
func makeUIView(context:Context)->WKWebView{
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlString, baseURL: nil)
uiView.isOpaque = false
}
}
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
,我的视图我会这样使用它
HTMLView(htmlString: "<html<<head><style>body{font-size:36px;color:#2282b2;blockquote {margin: 0 auto;padding: 1em;border-left: 5px solid #2282b2;font-size: 12pt;line-height: 1.4;color:#2282b2;} font-family:\"AmericanTypewriter\"}</style></head><body><h1>\(message.title!)</h1>\(message.content!)</body></html>" )
.padding()
,但是最新更新后不断收到消息 'type'webview'不符合协议“ uiview -rementable”,
请建议或举例说明如何在SwiftUI最新版本中使用WKWebView(ios 15)
Previously I used the following code to render HTML in a view with swiftUI
import WebKit
struct HTMLView: UIViewRepresentable {
let htmlString:String
func makeUIView(context:Context)->WKWebView{
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlString, baseURL: nil)
uiView.isOpaque = false
}
}
struct HTMLView: UIViewRepresentable {
let htmlString:String
func makeUIView(context:Context)->WKWebView{
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlString, baseURL: nil)
uiView.isOpaque = false
}
}
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
and i my view I use it like this
HTMLView(htmlString: "<html<<head><style>body{font-size:36px;color:#2282b2;blockquote {margin: 0 auto;padding: 1em;border-left: 5px solid #2282b2;font-size: 12pt;line-height: 1.4;color:#2282b2;} font-family:\"AmericanTypewriter\"}</style></head><body><h1>\(message.title!)</h1>\(message.content!)</body></html>" )
.padding()
This was working great but after latests updates keep getting message
'Type 'WebView' does not conform to protocol 'UIViewRepresentable'
Please advice or give example how to use WKWebview in swiftUI latest version (iOS 15)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是与您的代码一起使用
WKWebView
的一个示例:在MacOS 12.4上,使用Xcode 13.3,Targets ios-15 MacCatalyst 12.3,仅在真实设备上进行了测试。请勿使用强制解开
!
。this is an example of using
WKWebview
with your code:On macos 12.4, using Xcode 13.3, targets ios-15 macCatalyst 12.3, tested on real devices only. Do not use forced unwrapping
!
.