SwiftUI App State

SwiftUI App State

When we create a new App in Swift UI, Xcode generates two template files for us:

  1. <ProjectName>App file containing a struct that conforms to App protocol and WindowGroup with ContentView(RootView)
  2. ContentView, a sample view with just a Text showing “Hello, world!”.

Unlike UIKit Template, where we get App state change methods by default, in SwiftUI we don’t have such methods.

But there is a way to track App State changes:

  1. Declare an Environment variable of scene phase

2. Add an onChange modifier for scenePhase and switch over ScenePhase types to handle states accordingly

Please share your thoughts and suggestions on this article.


要查看或添加评论,请登录

Amit Ranjan ?的更多文章

社区洞察

其他会员也浏览了