// // URLDisplayAppApp.swift // URLDisplayApp // // Created by Eli Slothower on 3/12/25. // import SwiftUI import SwiftData // You can keep NavigationManager here or move it to its own file. class NavigationManager: ObservableObject { static let shared = NavigationManager() @Published var sharedURL: URL? = nil func navigateToURL(_ url: URL) { sharedURL = url } } // The AppDelegate that handles deep linking import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { print("AppDelegate triggered with URL: \(url)") // (Your URL parsing and handling code follows here) guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), components.scheme == "myapp", components.host == "share", let queryItem = components.queryItems?.first(where: { $0.name == "url" }), let sharedURLString = queryItem.value, let sharedURL = URL(string: sharedURLString) else { return false } NavigationManager.shared.navigateToURL(sharedURL) return true } } @main struct MyApp: App { // Connect your AppDelegate to the SwiftUI lifecycle. @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate @StateObject private var navManager = NavigationManager.shared var body: some Scene { WindowGroup { ContentView() .environmentObject(navManager) .onOpenURL { url in // Optional fallback if needed; the AppDelegate should already handle it. handleSharedURL(url) } } } private func handleSharedURL(_ url: URL) { guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), components.scheme == "myapp", components.host == "share", let queryItem = components.queryItems?.first(where: { $0.name == "url" }), let sharedURLString = queryItem.value, let sharedURL = URL(string: sharedURLString) else { return } navManager.navigateToURL(sharedURL) } }