How to know when an app or WindowGroup is in the background?

I'm unable to figure out how to know when my app no longer has focus. ScenePhase will only change when the WindowGroup gets created or closed.

UIApplication.didBecomeActive and UIApplication.didEnterBackgroundNotification are not called either when say you move focus to Safari.

What's the trick?

Answered by Vision Pro Engineer in 786928022

The ScenePhase property will inform you on the scene’s operational state. Your scene's operational state may transition to the .inactive and .background states while the window exists in the shared space. When read on a view, it returns the phase of the scene that contains the view; when read within an App instance, you obtain an aggregate value that reflects the phases of all the scenes in your app. Take a look at the documentation for more details.

Could you provide more information as to what you are trying to do and what scenarios you want to account for? Perhaps there's a more appropriate API or design pattern to apply to your situation. The Human Interface Guidelines, Restoring Your App’s State sample code, and Preserving your app’s UI across launches article all highlight the importance of preserving app state:

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.

The user should be able to return to where they left off — and UI state restoration is a core part of making that experience seamless.

Preserving your app’s user interface helps maintain the illusion that your app is always running.

Thanks, Michael

Making ControlActiveState available on visionOS would solve this issue. It's only available on macOS at this time.

FB13675635

The ScenePhase property will inform you on the scene’s operational state. Your scene's operational state may transition to the .inactive and .background states while the window exists in the shared space. When read on a view, it returns the phase of the scene that contains the view; when read within an App instance, you obtain an aggregate value that reflects the phases of all the scenes in your app. Take a look at the documentation for more details.

Could you provide more information as to what you are trying to do and what scenarios you want to account for? Perhaps there's a more appropriate API or design pattern to apply to your situation. The Human Interface Guidelines, Restoring Your App’s State sample code, and Preserving your app’s UI across launches article all highlight the importance of preserving app state:

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.

The user should be able to return to where they left off — and UI state restoration is a core part of making that experience seamless.

Preserving your app’s user interface helps maintain the illusion that your app is always running.

Thanks, Michael

How to know when an app or WindowGroup is in the background?
 
 
Q