adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.23k stars 107 forks source link

Deeplink in IOS #382

Open ghasemdev opened 1 month ago

ghasemdev commented 1 month ago

I need to implement deep linking with URL Scheme in iOS, but how can I connect that native Swift code to Voyager? The documentation suggests adding this function to the AppDelegate. How can I create this delegate class and connect it to other classes?

func application(_ app: UIApplication, open url: URL,
                 options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if let scheme = url.scheme,
        scheme.localizedCaseInsensitiveCompare("com.myApp") == .orderedSame,
        let view = url.host {

        var parameters: [String: String] = [:]
        URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems?.forEach {
            parameters[$0.name] = $0.value
        }

        redirect(to: view, with: parameters) // this line how navigate with voyager??
    }
    return true
}
@main
struct iOSApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
struct ContentView: View {
    var body: some View {
        ComposeView()
                .ignoresSafeArea(.keyboard) // Compose has own keyboard handler
    }
}

 ```swift
struct ComposeView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> UIViewController {
        MainViewControllerKt.MainViewController()
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
fun MainViewController() = ComposeUIViewController { App() }
 @Composable 
 fun App () {
     Navigator(ListScreen()) { navigator ->
          CurrentScreen()
     }
}
DevSrSouza commented 4 weeks ago

@ghasemdev there is no currently built-in way of handling deeplink uris in voyager. The issue #387 has a example how you can handle this your self with replaceAll for example. Also this library from the same author of the issue show how to easily handle deeplink with Voyager and Compose KMP https://github.com/theolm/Rinku

We plan to add better docs for deeplink examples.