如何使用最新版本的Swiftui使用WKWebView?

发布于 2025-01-31 03:48:19 字数 1748 浏览 5 评论 0原文

以前,我使用以下代码在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2025-02-07 03:48:19

这是与您的代码一起使用WKWebView的一个示例:

struct Message {
    var title: String?
    var content: String?
}

struct ContentView: View {
    @State var message = Message(title: "The message title", content: "The message content")
    
    var body: some View {
        // -- note the default values for `message`
        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 ?? "no title")</h1>\(message.content ?? "no content")</body></html>")
            .padding()
    }
}

在MacOS 12.4上,使用Xcode 13.3,Targets ios-15 MacCatalyst 12.3,仅在真实设备上进行了测试。请勿使用强制解开

this is an example of using WKWebview with your code:

struct Message {
    var title: String?
    var content: String?
}

struct ContentView: View {
    @State var message = Message(title: "The message title", content: "The message content")
    
    var body: some View {
        // -- note the default values for `message`
        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 ?? "no title")</h1>\(message.content ?? "no content")</body></html>")
            .padding()
    }
}

On macos 12.4, using Xcode 13.3, targets ios-15 macCatalyst 12.3, tested on real devices only. Do not use forced unwrapping !.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文