How to use WebView in SwiftUI

Issue #736

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
struct MyWebView: NSViewRepresentable {
let url: URL
@Binding
var isLoading: Bool

func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}

func makeNSView(context: Context) -> WKWebView {
let view = WKWebView()
view.navigationDelegate = context.coordinator
view.load(URLRequest(url: url))
return view
}

func updateNSView(_ nsView: WKWebView, context: Context) {

}

class Coordinator: NSObject, WKNavigationDelegate {
let parent: MyWebView

init(parent: MyWebView) {
self.parent = parent
}

func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
parent.isLoading = true
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
parent.isLoading = false
}
}
}

Comments