Download this file
| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// Another app asking to open Base: `works.oficina.base://open`, or |
| 5 | /// `…://open?path=/a/page`, handled by the kit's NativeConnection (its |
| 6 | /// `onOpen`, Task 1), which holds the path to a page on Base's own origin. |
| 7 | /// Base comes forward with Ask; a page Base doesn't draw natively (Store, |
| 8 | /// People) opens on the web. |
| 9 | @MainActor public final class BaseOpenURL { |
| 10 | public static let scheme = "works.oficina.base" |
| 11 | private let connection: NativeConnection |
| 12 | |
| 13 | public init(defaults: UserDefaults = UserDefaults(suiteName: "works.oficina.base.open") ?? .standard, |
| 14 | showAsk: @escaping @MainActor () -> Void, openWeb: @escaping @MainActor (String) -> Void) { |
| 15 | connection = NativeConnection(defaultServer: "https://base.oficina.works", callbackScheme: Self.scheme, defaults: defaults) |
| 16 | connection.onOpen = { path in |
| 17 | guard let path, path != "/", path != "/ask" else { showAsk(); return } |
| 18 | openWeb(path) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | /// Hand every URL the app is opened with here; anything else is ignored. |
| 23 | public func handle(_ url: URL) { connection.handleOpenURL(url) } |
| 24 | } |
| 25 | |