How is "Navigator" implemented in the session's video?

I'm trying to figure out what the actual logic to navigate my App's screens is (I know the video says we shouldn't use it for that - this is just an exercise) using intents, but the lack of the Navigator implementation is a show-stopper. Can someone kindly share what the implementation for it looks like?

Accepted Reply

I figured it out thank to last year's Accelerating App Interactions with App Intents sample code.

Essentially you can inject your app's navigation model to the Intent by using dependencies, namely the @AppDependency property wrapper and the AppDependencyManager class.

Replies

In SwiftUI, you typically use NavigationView and NavigationLink for navigation between screens or views. While it's not recommended to use intents for general navigation within your app, you can implement basic navigation like this:

  1. Import SwiftUI:
import SwiftUI
  1. Create your views. For example, you might have two views, ViewA and ViewB:
struct ViewA: View {
    var body: some View {
        NavigationView {
            VStack {
                Text("View A")
                NavigationLink("Go to View B", destination: ViewB())
            }
        }
    }
}

struct ViewB: View {
    var body: some View {
        Text("View B")
    }
}
  1. In your @main App struct, set the ViewA as the initial view:
@main
struct YourApp: App {
    var body: some Scene {
        WindowGroup {
            ViewA()
        }
    }
}
  1. Run your app. You'll see that you can navigate from View A to View B by tapping "Go to View B."

This is a basic example of navigation in SwiftUI. If you have more complex navigation needs, you can use NavigationView, NavigationLink, and the @State property wrapper to manage the navigation state within your views. Intents are typically used for handling app-specific actions triggered by Siri or other external events rather than general navigation within your app.

  • This doesn't really answer the question. I am aware what intents are for and how navigation works in SwiftUI. However in the WWDC Session from this year (10032) they clearly say it's possible. May not be recommended (which I also state in the first post), but it's possible. I just need to understand how the "Navigator" object is implemented so I can better understand how the "perform" method works in general.

  • Perhaps the context of the question got lost because it wasn't tagged as wwdc2022-10032 when I opened the forums from the Developer app, my bad.

Add a Comment

I figured it out thank to last year's Accelerating App Interactions with App Intents sample code.

Essentially you can inject your app's navigation model to the Intent by using dependencies, namely the @AppDependency property wrapper and the AppDependencyManager class.