How to get initial WindowGroup to reopen on launch (visionOS)

In my visionOS app, I have an initial WindowGroup (no parameters), which opens correctly on application launch. I have multiple other WindowGroup(for:) closures in the app, too.

If I open one of the other WindowGroup(for:) windows, then close the initial window, I can't get the initial window back. If I close the second window, then tap on app icon, the second window reappears, but the initial window isn't shown.

The only way to get the initial window back, once it's closed, is to force-quit the app and restart.

A one-file reproducible example is below. In this example, the "button tapped" window is the second window. What do I need to do to get the initial window back?

Note this problem is in a visionOS app. But I've been able to reproduce even if I remove the RealityKitContent package from the target.

If I close, say, Safari's only window, then Safari exits, and shows me its window when I relaunch. The behavior I'd like to see is that when I "click" the app icon, the app presents its main window again.

I have also tried giving the WindowGroup an id. That does allow me to openWindow from another window (could use a toolbar item for that), but it doesn't get the main window to appear on launch, and I end up with multiple copies of the Main window.

import SwiftUI

@main
struct Limbo_MREApp: App {
    var body: some Scene {
// I've also tried this form, same result
//      WindowGroup(id: "main") {
        WindowGroup {
            ContentView()
        }
        
        WindowGroup(for: String.self) { $string in
            if let string {
                Text(string)
                    .font(.extraLargeTitle2)
            }
        }
    }
}

struct ContentView: View {
    @Environment(\.openWindow) var openWindow

    var body: some View {
        VStack {
            Text("Hello, world!")
            Button("Tap Me", systemImage: "doc.on.doc") {
                openWindow(value: "button tapped")
            }
        }
    }
}
Answered by Vision Pro Engineer in 789464022

Hi @halm ,

This is a known issue right now. As a workaround, you can use .handlesExternalEvents(preferring:, allowing:) and put it on the scenes you do not want to open.

var body: some Scene {
        WindowGroup(id: "main") {
            ContentView()
        }
        
        WindowGroup(id: "Secondary") {
            SecondaryView()
                .handlesExternalEvents(preferring: [], allowing: [])
        }
    }

Note: This workaround will open both windows on relaunch.

Make sure to verify the behavior when running your app by going to "Product > Copy to device" in the Xcode menu bar. Then, launch the app from Home.

Accepted Answer

Hi @halm ,

This is a known issue right now. As a workaround, you can use .handlesExternalEvents(preferring:, allowing:) and put it on the scenes you do not want to open.

var body: some Scene {
        WindowGroup(id: "main") {
            ContentView()
        }
        
        WindowGroup(id: "Secondary") {
            SecondaryView()
                .handlesExternalEvents(preferring: [], allowing: [])
        }
    }

Note: This workaround will open both windows on relaunch.

Make sure to verify the behavior when running your app by going to "Product > Copy to device" in the Xcode menu bar. Then, launch the app from Home.

Is that workaround still the only solution in 2.1?

I have a scene with one anonymous Window group, and 4 Window groups with ids.

When I have main and one other WindowGroup open, and close the anonymous window first, then on reopening 2 of the WindowGroups with id appear, yet not the main WindowGroup.

How to get initial WindowGroup to reopen on launch (visionOS)
 
 
Q