ahmad elkhwaga
3 min readOct 23, 2020

--

AppDelegate vs. SceneDelegate.

In Xcode 11 and later, you might have noticed that with the default files ( AppDelegate.swift, ViewController.swift and a StoryBoard) and a new file is created named as (SceneDelegate.swift).

Now we have a two major files for our apps lifecycle (AppDelegate.swift and SceneDelegate.swift), So from iOS 13 and after, the responsibilities of AppDelegate have been split between AppDelegate and SceneDelegate. let’s take a look into them and try to understand the difference between these two files.

Starting explanation

AppDelegate.swift

it uses the protocol UIApplicationDelegate.

UIApplicationDelegate:- It has a set of methods that you use to manage shared behaviors for your app.

Use your app delegate object to handle the following tasks:

  • Initializing your app’s central data structures.
  • Configuring your app’s scenes.
  • Responding to notifications originating from outside the app, such as low-memory warnings, download completion notifications, and more.
  • Responding to events that target the app itself, and are not specific to your app’s scenes, views, or view controllers.
  • Registering for any required services at launch time, such as Apple Push Notification service.

In the default AppDelegate.swift there are three methods that Apple considers to be important that we have to consider and let’s look into them

  • func application(_:didFinishLaunchingWithOptions:) -> Bool

Tells the delegate that the launch process has begun but that state restoration has not yet occurred.

  • func application(_:configurationForConnecting:options:) -> UISceneConfiguration

Returns the configuration data for UIKit to use when creating a new scene, This method is called when ever the application needs a new scene or window to display and need to be obtained.

  • func application(_:didDiscardSceneSessions:)

Tells the delegate that the user closed one or more of the app’s scenes from the app switcher.

Double-press the Home button or swipe up from the bottom of your iPhone and voilá, you’re seeing the windows of the apps that are currently running. This is called the app switcher.

SceneDelegate.swift

it uses the protocol UISceneDelegate.

UISceneDelegate:- The core methods you use to respond to life-cycle events occurring within a scene.

Use your UISceneDelegate object to manage life-cycle events in one instance of your app's user interface. This interface defines methods for responding to state transitions that affect the scene, including when the scene enters the foreground and becomes active, and when it enters the background.

The default methods that can be seen in SceneDelegate are:

  • scene(_:willConnectTo:options:)

This is the first method called in UISceneSession life cycle. This method is called when your app creates or restores an instance of your user interface.

  • sceneDidDisconnect(_:)

Tells the delegate that UIKit removed a scene from your app. This doesn’t meant that the app is killed or not running, but just the scene is disconnected from the session and is not active. This method can be used to discard any resources that aren’t used anymore.

  • sceneWillEnterForeground(_:)

Tells the delegate that the scene is about to begin running in the foreground and become visible to the user.

  • sceneDidBecomeActive(_:)

This method is called right after the WillEnterForeground method and Tells the delegate that the scene became active and is now responding to user events.

  • sceneWillResignActive(_:)

Tells the delegate that the scene is about to resign the active state and stop responding to user events (when app become in background mode).

  • sceneDidEnterBackground(_:)

Tells the delegate that the scene is running in the background and is no longer onscreen.

Conclusion

The main reason for Apple to add UISceneDelegate to iOS 13 was to split responsibilities between AppDelegate and SceneDelegate, AppDelegate’s roles is for handling application-level events, like app launch and the SceneDelegate’s role is for scene lifecycle events like scene creation, destruction and handling background and foreground modes.

--

--