Branchwell

Docley / app / Apps / iOS / WebSheet.swift

1import SwiftUI
2import WebKit
3
4/// Signing in, rating, claiming and replying happen on the website, inside the app.
5/// Sign-in is passkeys only. The webcredentials associated domain (Docley.entitlements) lets WebAuthn run in this web view.
6struct WebSheet: View {
7 let url: URL
8 @Environment(\.dismiss) private var dismiss
9 var body: some View {
10 NavigationStack {
11 WebView(url: url)
12 .ignoresSafeArea(edges: .bottom)
13 .navigationTitle("Docley").navigationBarTitleDisplayMode(.inline)
14 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { dismiss() } } }
15 }
16 }
17}
18
19struct WebView: UIViewRepresentable {
20 let url: URL
21 func makeUIView(context: Context) -> WKWebView {
22 let config = WKWebViewConfiguration()
23 config.websiteDataStore = .default() // keeps the Docley sign-in between visits
24 let view = WKWebView(frame: .zero, configuration: config)
25 view.isOpaque = false
26 view.backgroundColor = .black
27 view.navigationDelegate = context.coordinator
28 view.load(URLRequest(url: url))
29 return view
30 }
31 func updateUIView(_ view: WKWebView, context: Context) {}
32 func makeCoordinator() -> Coordinator { Coordinator() }
33
34 final class Coordinator: NSObject, WKNavigationDelegate {
35 // Stay on Docley; open everything else (sources, projectatom.ai) in Safari.
36 func webView(_ webView: WKWebView, decidePolicyFor action: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
37 if let u = action.request.url, u.host != Site.base.host, action.navigationType == .linkActivated {
38 UIApplication.shared.open(u); decisionHandler(.cancel); return
39 }
40 decisionHandler(.allow)
41 }
42 }
43}