Posts

Post not yet marked as solved
0 Replies
146 Views
When I learned to use webview in my app, just added a webview and detected a memory leak, import SwiftUI import WebKit struct SWKWebView: UIViewRepresentable { @Binding var url: String? func makeUIView(context: Context) -> WKWebView { let webview = WKWebView() webview.navigationDelegate = context.coordinator return webview } func updateUIView(_ uiView: WKWebView, context: Context) { if let url = url, let requetURL = URL(string: url) { uiView.load(URLRequest(url: requetURL)) } } func makeCoordinator() -> Coordinator { Coordinator() } class Coordinator: NSObject,WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { webView.evaluateJavaScript("document.title") { (result, error) in print("didFinish:\(String(describing: result ?? ""))") } } } } struct TTTest: View { @State var url: String? = "https://www.google.com" var body: some View { SWKW ![]("https://developer.apple.com/forums/content/attachment/d4b6a729-f217-4b1a-9ec4-1372643e14b4" "title=WeChatc57286e9fa2eb9bc5d59b7410d876c33.png;width=1788;height=660") ebView(url: $url) } }
Posted
by acsa.
Last updated
.