In SwiftUI, it looks like when a window is not in view its scene phase is set to ScenePhase.background
after a minute or so. Now when every window of an app has been closed, the last window to be closed has its phase set to SchenePhase.background
. This makes it quite difficult to differentiate reopening and app from a window that was out of view being glanced at again.
I have tried implementing a solution where I count opened/closed window with an onChange
that watches scenePhase
, but it unfortunately looks like not every window closing gets detected (specifically if there are two windows, with one window is in the background, and the one active window is closed, its scene phase onChange is never triggered).
Is there a better way to handle the case of differentiating between reopening an app with looking back at a suspended window that was never actually closed?
The ScenePhase property reports the state of each scene that it is being read from. Please note that the ScenePhase
value depends on where you are reading this value from. The documentation has code samples of this property being read both on individual views and on the app as a whole. A view's ScenePhase will enter the .background state prior to termination of the scene that contains the view. Please verify you are observing the expected scene. If you encounter behavior contrary to the documentation, please file a bug report for this issue using Feedback Assistant and post the Feedback number here. Sample projects are always helpful.
Could you provide more information about what you are trying to accomplish? As a user, I would not expect different behavior from an application if I'm reopening the application versus keeping the window open. Perhaps there's another API or a better design pattern to apply here. Applications should attempt to restore the previous state as much as possible. The Human Interface Guidelines state the following:
On Apple Vision Pro, people can run multiple apps at the same time in the Shared Space, viewing and switching between windows and volumes throughout their space.
Preserve the state in each window that people open. When people return to a window, they expect it to be in the same state in which they left it. For developer guidance, see Restoring Your App’s State.
Thanks, Michael