|
| 1 | +import SwiftRs |
| 2 | +import Tauri |
| 3 | +import UIKit |
| 4 | +import WebKit |
| 5 | + |
| 6 | +struct SetEventHandlerArgs: Decodable { |
| 7 | + let handler: Channel |
| 8 | +} |
| 9 | + |
| 10 | +@objc class DeepLinkPlugin: Plugin { |
| 11 | + private var webView: WKWebView? |
| 12 | + private var currentUrl: String? |
| 13 | + private var channel: Channel? |
| 14 | + |
| 15 | + // Called when the plugin is loaded |
| 16 | + @objc public override func load(webview: WKWebView) { |
| 17 | + super.load(webview: webview) |
| 18 | + self.webView = webview |
| 19 | + DeepLinkPlugin.instance = self |
| 20 | + |
| 21 | + // Check if app was launched via URL |
| 22 | + if let url = DeepLinkPlugin.launchedUrl { |
| 23 | + // If so, send to JS |
| 24 | + var event = JSObject() |
| 25 | + event["url"] = url.absoluteString |
| 26 | + self.channel?.send(event) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // JS command: return current URL |
| 31 | + @objc public func getCurrent(_ invoke: Invoke) throws { |
| 32 | + var ret = JSObject() |
| 33 | + ret["url"] = self.currentUrl |
| 34 | + invoke.resolve(ret) |
| 35 | + } |
| 36 | + |
| 37 | + // JS command: set the JS callback handler channel |
| 38 | + @objc public func setEventHandler(_ invoke: Invoke) throws { |
| 39 | + let args = try invoke.parseArgs(SetEventHandlerArgs.self) |
| 40 | + self.channel = args.handler |
| 41 | + invoke.resolve() |
| 42 | + } |
| 43 | + |
| 44 | + // Static helpers to store/dispatch URLs |
| 45 | + static var instance: DeepLinkPlugin? |
| 46 | + static var launchedUrl: URL? |
| 47 | + static func handleOpenUrl(_ url: URL) { |
| 48 | + // Called from AppDelegate/SceneDelegate |
| 49 | + if let plugin = DeepLinkPlugin.instance { |
| 50 | + plugin.currentUrl = url.absoluteString |
| 51 | + if let ch = plugin.channel { |
| 52 | + var event = JSObject() |
| 53 | + event["url"] = url.absoluteString |
| 54 | + ch.send(event) |
| 55 | + } |
| 56 | + } else { |
| 57 | + // App not yet initialized; save for load() |
| 58 | + DeepLinkPlugin.launchedUrl = url |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +@UIApplicationMain |
| 64 | +class AppDelegate: UIResponder, UIApplicationDelegate { |
| 65 | + func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { |
| 66 | + DeepLinkPlugin.handleOpenUrl(url) |
| 67 | + return true |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +class SceneDelegate: UIResponder, UIWindowSceneDelegate { |
| 74 | + |
| 75 | + var window: UIWindow? |
| 76 | + |
| 77 | + // Called when a scene is being created and connected |
| 78 | + func scene(_ scene: UIScene, |
| 79 | + willConnectTo session: UISceneSession, |
| 80 | + options connectionOptions: UIScene.ConnectionOptions) { |
| 81 | + |
| 82 | + // Handle initial URL if app was launched via a deep link |
| 83 | + if let urlContext = connectionOptions.urlContexts.first { |
| 84 | + DeepLinkPlugin.handleOpenUrl(urlContext.url) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Called when the app receives a deep link while already running |
| 89 | + func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { |
| 90 | + guard let url = URLContexts.first?.url else { return } |
| 91 | + DeepLinkPlugin.handleOpenUrl(url) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@_cdecl("init_plugin_deep_link") |
| 96 | +func initPlugin() -> Plugin { |
| 97 | + return DeepLinkPlugin() |
| 98 | +} |
| 99 | + |
| 100 | +// import Tauri |
| 101 | +// import UIKit |
| 102 | +// import WebKit |
| 103 | + |
| 104 | +// class PingArgs: Decodable { |
| 105 | +// let value: String? |
| 106 | +// } |
| 107 | + |
| 108 | +// class ExamplePlugin: Plugin { |
| 109 | +// @objc public func ping(_ invoke: Invoke) throws { |
| 110 | +// let args = try invoke.parseArgs(PingArgs.self) |
| 111 | +// invoke.resolve(["value": args.value ?? ""]) |
| 112 | +// } |
| 113 | +// } |
0 commit comments