Scene Phase issue with VisionOS 2.0

Hello,

I am new to swiftUI and VisionOS but I developed an app with a window and an ImmersiveSpace. I want the Immersive space to be dismissed when the window/app is closed.

I have the code below using the state of ScenePhase and it was working fine in Vision OS 1.1 but it stopped working with VisionOS 2.0.

Any idea what I am doing wrong? Is there another way to handle the dismissal of ImmersiveSpace when my main Window is closed?



@main
struct MyApp: App {
    
    @State private var viewModel = ViewModel()

    var body: some Scene {
        
        @Environment(\.scenePhase) var scenePhase
        @Environment(\.dismissImmersiveSpace) var dismissImmersiveSpace
        
        WindowGroup {
            SideBarView()
                .environment(viewModel)
                .frame(width: 1150,height: 700)
                .onChange(of: scenePhase, { oldValue, newValue in
                    if newValue == .inactive || newValue == .background {
                        Task {
                            await dismissImmersiveSpace()
                            viewModel.immersiveSpaceIsShown = false
                        }
                    }
                })

        }.windowResizability(.contentSize)
           
        ImmersiveSpace(id: "ImmersiveSpace") {
            ImmersiveView(area: viewModel.currentModel)
                .environment(viewModel)
       }      

    }
}

Answered by Mike91601 in 791738022

I corrected as per your recommendation, it still had no effect. But I manage to get it to work by moving the "onchange" modifier inside the view itself.

Hi,

The above code didn't compile for me but changing it to something like this worked:

            .onChange(of: scenePhase) { (oldValue, newValue) in
                if newValue == .inactive || newValue == .background {
                    Task {
                        await dismissImmersiveSpace()
                        viewModel.immersiveSpaceIsShown = false
                    }
                }
            }
Accepted Answer

I corrected as per your recommendation, it still had no effect. But I manage to get it to work by moving the "onchange" modifier inside the view itself.

Scene Phase issue with VisionOS 2.0
 
 
Q