-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebView.swift
57 lines (47 loc) · 2.09 KB
/
WebView.swift
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// WebView.swift
import SwiftUI
import WebKit
struct WebView: NSViewRepresentable {
let webView: WKWebView
@ObservedObject var webViewStateModel: WebViewStateModel
var updateTabTitle: (String?) -> Void
var updateTabURL: (String?) -> Void
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: Context) -> WKWebView {
webView.navigationDelegate = context.coordinator
webView.uiDelegate = context.coordinator
return webView
}
func updateNSView(_ nsView: WKWebView, context: Context) {
// Update the webView's content if needed
// Since we're passing the webView instance, this may not be necessary
}
class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
var parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
parent.webViewStateModel.canGoBack = webView.canGoBack
parent.webViewStateModel.canGoForward = webView.canGoForward
parent.updateTabTitle(webView.title)
parent.updateTabURL(webView.url?.absoluteString)
parent.webViewStateModel.pageURL = webView.url?.absoluteString
parent.webViewStateModel.pageTitle = webView.title
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
parent.webViewStateModel.canGoBack = webView.canGoBack
parent.webViewStateModel.canGoForward = webView.canGoForward
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
parent.webViewStateModel.canGoBack = webView.canGoBack
parent.webViewStateModel.canGoForward = webView.canGoForward
parent.updateTabTitle(webView.title)
parent.updateTabURL(webView.url?.absoluteString)
parent.webViewStateModel.pageURL = webView.url?.absoluteString
parent.webViewStateModel.pageTitle = webView.title
}
}
}