|
| 1 | +// |
| 2 | +// WebView.swift |
| 3 | +// Smth |
| 4 | +// |
| 5 | +// Created by Tony Clark on 2023/9/29. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import WebKit |
| 10 | + |
| 11 | +#if os(iOS) |
| 12 | +typealias WebViewRepresentable = UIViewRepresentable |
| 13 | +#elseif os(macOS) |
| 14 | +typealias WebViewRepresentable = NSViewRepresentable |
| 15 | +#endif |
| 16 | + |
| 17 | +struct WebView: WebViewRepresentable { |
| 18 | + |
| 19 | + let url: URL |
| 20 | + let onComplete: () -> Void? |
| 21 | +// @Binding var isLoading: Bool |
| 22 | +// @Binding var error: Error? |
| 23 | + |
| 24 | + func makeCoordinator() -> Coordinator { |
| 25 | + Coordinator(self) |
| 26 | + } |
| 27 | +#if os(iOS) |
| 28 | + func makeUIView(context: Context) -> WKWebView { |
| 29 | + makeView(context: context) |
| 30 | + } |
| 31 | + |
| 32 | + func updateUIView(_ uiView: WKWebView, context: Context) { |
| 33 | + // This space is left intentionally blank. |
| 34 | + } |
| 35 | +#endif |
| 36 | + |
| 37 | +#if os(macOS) |
| 38 | + public func makeNSView(context: Context) -> WKWebView { |
| 39 | + makeView(context: context) |
| 40 | + } |
| 41 | + |
| 42 | + public func updateNSView(_ view: WKWebView, context: Context) { |
| 43 | + |
| 44 | + } |
| 45 | +#endif |
| 46 | + |
| 47 | + private func makeView(context: Context) -> WKWebView { |
| 48 | + let wkwebView = WKWebView() |
| 49 | + wkwebView.navigationDelegate = context.coordinator |
| 50 | + wkwebView.load(URLRequest(url: url)) |
| 51 | + return wkwebView |
| 52 | + } |
| 53 | + |
| 54 | + class Coordinator: NSObject, WKNavigationDelegate { |
| 55 | + var parent: WebView |
| 56 | + |
| 57 | + init(_ parent: WebView) { |
| 58 | + self.parent = parent |
| 59 | + } |
| 60 | + |
| 61 | + func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { |
| 62 | + //parent.isLoading = true |
| 63 | + } |
| 64 | + |
| 65 | + func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { |
| 66 | + //parent.isLoading = false |
| 67 | + } |
| 68 | + |
| 69 | + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 70 | + webView.evaluateJavaScript("document.cookie") { (result, error) in |
| 71 | + if let cookieString = result as? String { |
| 72 | + print("Cookies: \(cookieString)") |
| 73 | + // 在这里处理获取到的 Cookie |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy { |
| 79 | + let cookies = try? await WKWebsiteDataStore.default().httpCookieStore.allCookies() |
| 80 | + var isLoggedIn = false |
| 81 | + cookies?.forEach { cookie in |
| 82 | + HTTPCookieStorage.shared.setCookie(cookie) |
| 83 | + if cookie.name == "kbs-key" { |
| 84 | + isLoggedIn = true |
| 85 | + self.parent.onComplete() |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if isLoggedIn { |
| 90 | + LoginState.shared.markLoggedIn() |
| 91 | + } else { |
| 92 | + LoginState.shared.markLoggedOut() |
| 93 | + } |
| 94 | + return .allow |
| 95 | + } |
| 96 | + |
| 97 | + func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { |
| 98 | + print("loading error: \(error)") |
| 99 | +// parent.isLoading = false |
| 100 | +// parent.error = error |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments