Home > Mobile >  Can some explain the difference with Xcode SceneDelegate vs App file
Can some explain the difference with Xcode SceneDelegate vs App file

Time:12-09

I have created an app for iOS in 2020, where when I started a brand new app with Swift and SwiftUI there would be two specific files:

1. SceneDelegate and 
2. ContentView

I have since downloaded Xcode and was looking to update my current app and noticed that the two base files now are:

1. <App Name>App and
2. ContentView

Why was the change made? And has that changed any behaviors?

Would I be able to "copy and paste" my code from the "old" app to the "new" app version? or has there been architectural changes that need to be accounted for?

CodePudding user response:

The change was made because of the new App protocol:

The amount of changes required depends on the complexity of your SceneDelegate. You can use the Scene type's onChange(of:perform:) method to handle events you would've previously handled in your SceneDelegate:

https://www.iosdevie.com/p/swiftui-replace-appdelegate-scenedelegate

@main
struct MyApp: App {
     @Environment(\.scenePhase) private var scenePhase
 
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .onChange(of: scenePhase) { phase in
             if phase == .background {
                //perform cleanup
             }
         }
     }
 }
  • Related