Define main window in visionOS

Hello everyone!

For applications with multiple windows, if I close all open windows, I've noticed that visionOS reopen only the last closed window when launching the app again from the menu.

Is there a way I can set a main window that will always be open when the application is launched?

Answered by Vision Pro Engineer in 787851022

Checkout the scene modifier handlesexternalevents(matching:) and the view modifier handlesExternalEvents(preferring:allowing:). Both pages include some examples on how these modifiers can be used.

Consider adding .handlesExternalEvents(preferring: [], allowing: []) to the root view of your secondary window. With this in place, if the user closes your app with the secondary window open, upon relaunch of the application, a new scene will be created containing the first WindowGroup.

Let me know if this helps,

Michael

Did you ever figure this out? I'm running into the same issue.

And even if the first window to open includes openWindow commands, they seem to not execute, as is the case when the LaunchView is the first to open when relaunching this example app. My Application Scene Manifest's (UIApplicationSceneManifest) Preferred Default Scene Session Role (UIApplicationPreferredDefaultSceneSessionRole) is Window Application Session (UIWindowSceneSessionRoleApplication).

import SwiftUI


@main
struct ExampleApp: App {
    var body: some Scene {
        WindowGroup {
            LaunchView()
        }
        
        WindowGroup(id: "GameControlWindow") {
            GameControlWindow()
                .padding()
        }
        
        WindowGroup(id: "GameVolume") {
            GameView()
        }
        .windowStyle(.volumetric)
    }
}


struct LaunchView: View {
    @Environment(\.openWindow) var openWindow
    
    var body: some View {
        EmptyView()
            .onAppear() {
                openWindow(id: "GameControlWindow")
                openWindow(id: "GameVolume")
            }
    }
}

Were you ever able to find a workaround for this issue?

Apple folks: FB13792217

Checkout the scene modifier handlesexternalevents(matching:) and the view modifier handlesExternalEvents(preferring:allowing:). Both pages include some examples on how these modifiers can be used.

Consider adding .handlesExternalEvents(preferring: [], allowing: []) to the root view of your secondary window. With this in place, if the user closes your app with the secondary window open, upon relaunch of the application, a new scene will be created containing the first WindowGroup.

Let me know if this helps,

Michael

Define main window in visionOS
 
 
Q