Difference between SceneDelegate and AppDelegate
Asked Answered
E

3

97

In my SwiftUI project I see AppDelegate file as well as a SceneDelegate file.

What are the differences between them?

For example between the methods in SceneDelegate

scene(_:willConnectTo:options:)

and in the AppDelegate

application(_:didFinishLaunchingWithOptions:)
Edson answered 7/6, 2019 at 16:24 Comment(1)
According to the talks, they took the functionality of AppDelegate from before and split it up. The SceneDelegate is specifically responsible for managing the active window, including displaying views.Hostel
S
119

The two files are meant to split the work by what is needed to run the app as a whole and what is needed for one "instance" that would support visibly running in the background. This would be something like configuring a database once, but displaying different sets of values by window.

You could think of them as the global and private versions. One is shared and the other is limited to the individual owner. In a way, they are exactly what you would expect by the names.

Multi-window support is happening

Next time you create a new Xcode project you’ll see your AppDelegate has split in two: AppDelegate.swift and SceneDelegate.swift. This is a result of the new multi-window support that landed with iPadOS, and effectively splits the work of the app delegate in two.

From iOS 13 onwards, your app delegate should:

  1. Set up any data that you need for the duration of the app.
  2. Respond to any events that focus on the app, such as a file being shared with you.
  3. Register for external services, such as push notifications.
  4. Configure your initial scenes.

In contrast, scene delegates are there to handle one instance of your app’s user interface. So, if the user has created two windows showing your app, you have two scenes, both backed by the same app delegate.

Keep in mind that these scenes are designed to work independently from each other. So, your application no longer moves to the background, but instead individual scenes do – the user might move one to the background while keeping another open.

Courtesy of https://www.hackingwithswift.com/articles/193/whats-new-in-ios-13

Sallee answered 7/6, 2019 at 16:43 Comment(8)
What if my app supports only iPhone?Damselfly
@ZaidPathan As the article said, it was only a result of support that was added with the release of iPadOS.Sallee
I'm very curious if it will have an impact on how iPhone applications are designed. I can imagine a game in which there is one scene for the menu and another scene for gameplay. Another practical usage of distinct window with scenedelegate can be checkout for e-commerce apps... Or am I missing someting? :DMeara
@Meara I think the word "scenes" may have confused you. This change divides the code from the app delegate that needs to be run with each instance from code that should only be run once for all instances. It's really that simple.Sallee
Where should user authentication logic go? Per statement about "Set up any data that you need for the duration of the app", auth should go into AppDelegate. But initial app entry logic only works if it's in SceneDelegate.scene, so I have to listen for user auth state in SceneDelegate as wellKesley
@AbandonedCart could you take a look at the question here: #61220584? It's not the same organization as you suggested in the comment, but I have the same problem.Kesley
I think it can be used in iPhoneOS moving forward especially on bigger screens where you might be able to split the screen horizontally in landscapeBurundi
@BodaTaljo To reiterate the comments from a year ago, it only arrived with iPadOS, but then expanded to iPhoneOS.Sallee
I
10

AppDelegate is responsible for handling application-level events(like app launch), application lifecycle, and setup.

SceneDelegate is responsible for handling what is shown on the screen (Windows or Scenes) and managing the way how your app is shown.

scene(_:willConnectTo:options:) is the first method called in UISceneSession life cycle. This method will create a new UIWindow, set the root view controller, and make this window the key window to be displayed.

application(_:didFinishLaunchingWithOptions:) is called when the application is launched and where the application set-up is done. Earlier iOS 13, we might have used this method to configure the UIWindow object and assign a ViewController instance to the UIWindow object to make it display on the screen. From iOS 13, if your application has scenes, then AppDelegate is no longer responsible for handling this and is moved to SceneDelegate.

From: https://medium.com/@kalyan.parise/understanding-scene-delegate-app-delegate-7503d48c5445

Ingunna answered 4/1, 2022 at 19:35 Comment(0)
G
2

Multiplatform

In addition to the answer of Abandoned Cart, Since Xcode 11, You have a new option called Multiplatform for choosing as a starting template. That's where you will only see a file contains:

@main
struct MyMultiplatformApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

This is how the system knows where to start the code with @main (in Swift 5.3) and it contains WindowGroup that manages multiple windows of your app in all apple platforms. So you don't need the be worry about SceneDelegate and AppDelegate anymore.

If you need it to be like the old app delegate, for example when you want to use its methods, You should subscribe for corresponding notifications or use the UIAppDelegateAdapter wrapper as I described here

Gossett answered 9/7, 2020 at 11:41 Comment(1)
You should subscribe for corresponding notifications or use the UIAppDelegateAdapter wrapper as I described here @IxxGossett

© 2022 - 2024 — McMap. All rights reserved.