Member-only story
SwiftUI: How to detect if the application in background or foreground?
How to use scenePhase to detect scene state? — iOS 14 — XCode 12
Most of the apps have use case to detect app state like background, foreground etc. With iOS 14, The app delegate and scene delegates are not part of code template. To manage scene state apple introduced a new environment variable called scene phase. But before we go to implement scene phase it is important to know different between scene state and App state.
Scene State vs App State
As new SwiftUI supports Mac, iPhone, and iPad app. every separate window considered as new scene. And scene phase determine the state of that scene not the app. For, iPhone app, we have only one Scene hence a scene state can be treated as app state.
Implementing Scene state:
For iPads (devices with possibility of having multiple scene):
We can always adapt AppDelegate to to get App state like following