Opening a SwiftUI app window to a particular place via intent?

It is possible via the new AppIntents framework to open your app from via a shortcuts intent, but I am currently very confused about how to ensure that a particular window is opened in a SwiftUI-runtime app.

If the use says "Open View A" via a shortcuts, or Siri, I'd like to make sure it opens the window for "View A", though a duplicate window could be acceptable too.

The WWDC22 presentation has the following:

    @MainActor
    func perform() async throws -> some IntentResult {
        Navigator.shared.openShelf(.currentlyReading)
        return .result()
    }

Where, from the perform method of the Intent structure, they tell an arbitrary Navigator (code not provided) to just open a view of the app. (How convenient!)

But for a multiwindow swiftUI app, I'm not sure how to make this work. @Environment variables are not available within the Intent struct, and even if I did have a "Navigator Singleton", I'm not sure how it could get the @Environment for openWindow since it's a View environment. AppIntents exist outside the View environment tree AFAIK.

Any Ideas? I'd be a little shocked if this is a UIKit only sort of thing, but at the same time... ya never know.

I think only OpenWindow works and you can maybe...communicate with your app through some sort of thingy like AppStorage or NotificationCenter? 🫠

As long as I know, even if it's UIKit sort of thing if you import it and it's a function that doesn't relate very much to generating a UI, it'll work. Folks have done things like modifying navigational view details and others through these.

Just did that for my own app, which had a pre-existing setup that made it easy.

Essentially, it's this

@main
struct MyApp: App {
    @StateObject private var appStateRegistry = AppStateRegistry.shared
    var body: some Scene {
        WindowGroup {
            MainView()
                .environmentObject(appStateRegistry)
                .environmentObject(appStateRegistry.tabStateHandler)
        }
    }
}

// See https://www.fivestars.blog/articles/app-state/ for why we need both AppStateRegistry and TabStateHandler
class AppStateRegistry: ObservableObject {
    static let shared = AppStateRegistry()
    var tabStateHandler = TabStateHandler()
}

public enum MainTab: String {
    case tab1, tab2, tab3
}

class TabStateHandler: ObservableObject {
    @Published var selection: MainTab = .tab1
}

struct MainView: View {
    @EnvironmentObject var tabStateHandler: TabStateHandler
    var body: some View {
        TabView(selection: $tabStateHandler.selection) {…}
    }
}

And now in your AppIntent all you need to do is

// It's not the same enum since you could have more (or less) options here
public enum ShortcutableView: String, AppEnum {
    case tab1, tab2
}

struct OpenViewIntent: AppIntent {
    static var openAppWhenRun: Bool = true // Make sure you have this

    @Parameter(title: "View")

    var view: ShortcutableView

    @MainActor
    func perform() async throws -> some IntentResult {
        switch view {
        case .tab1:
            AppStateRegistry.shared.tabStateHandler.selection = .tab1
        case .tab2:
            AppStateRegistry.shared.tabStateHandler.selection = .tab2
        }
        return .result()
    }
}
Opening a SwiftUI app window to a particular place via intent?
 
 
Q