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?

Answered by Andy Ibanez in 769565022

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.

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.

Accepted Answer

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.

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