Scene Delegate in iOS 13

Amit Kumar
Mac O’Clock
Published in
4 min readMay 28, 2020

Since iOS 13 scene delegate added automatically to every new project. What’s the role of scene delegate? We will see the use of scene delegate and how it works with app delegate in iOS app.

AppDelegate :

As we all know about app delegate. It is the starting point of application and responsible for app launch.

Responsibilities of app delegate on iOS 12:

  • Configure App settings, start components and other required services.
  • Set up the root View Controller of app.
  • Register and handle push notifications.
  • Handle app lifecycle events like background, foreground logics.
  • Handle app terminate event etc.

Lets have a look at new app delegate methods.

App Delegate Methods
  • func application(_:didFinishLaunchingWithOptions:) -> Bool {} → This method is called on app launched and all the required setup is done in this method. From iOS 13, if your application has scenes, then AppDelegate is no longer responsible for handling this and is moved to SceneDelegate.
  • func application(_:configurationForConnecting:options:) -> UISceneConfiguration {} → This function is called when it create a session object for this scene.
  • func application(_:didDiscardSceneSessions:) {} →This function is called when user discard a scene session.

Note : Apart from these new methods, AppDelegate can still handle the external services like location services, push notifications and more

Scene Delegate :

Important role of scene delegate is to replace the concept of window with scene. From iOS 13 and later, SceneDelegate takes up some responsibilites from AppDelegate. Now apps can have multiple scenes at the same time. So, the SceneDelegate is responsible for whatever displayed on the screen.

Note: A SceneDelegate class includes lifecycle event such as active, resign, reconnect, disconnect etc.

Let’s have a look at scene delegate methods.

Scene Delegate Methods
  • func scene(_:willConnectTo :options:) {} → This is the most important and first method to be called in UISceneSession cycle. So we can do the Configuration which we earlier used to do in didFinishLaunchingWithOptions method in app delegate.
  • func sceneDidDisconnect(_:) {} → This method called when a scene has been disconnected or reconnected.
  • func sceneDidBecomeActive(_:) {} → This method is called when scene become active and user starts interacting with a scene, such as selecting it from the app switcher
  • func sceneWillResignActive(_:) {} → This method is called when the user stops interacting with a scene or app stages to background.
  • func sceneWillEnterForeground(_:) {} → This method is called when a scene enters the foreground, i.e. starts or resumes from a background state
  • func sceneDidEnterBackground(_:) {} → This method is called when a scene enters the background, i.e. the app is minimized but still present in the background.

Application Scene Manifest :

Apart from sceneDelegate.swift file Xcode 11 also add some scene manifest configurations data in the Info.plist file. This sheet contains all the scenes our app support. We can edit the scene’s configurations, Class Name, Storyboard Name, values etc.

Every scene your app supports needs to be declared in an Application Scene Manifest

The most important information is kept inside the items in the Application Session Role array. This entry lists the following:

  • The name of this configuration, which needs to be unique
  • The class name of the scene, UIWindowScene
  • The class name of the delegate for this scene, which is normally SceneDelegate
  • The name of the storyboard that contains the initial UI for this scene.

Conclusion

Most important use of Scene Delegate to create multi window applications. Now its clear the use of AppDelegate and SceneDelegate in iOS 13 and their life cycle events. SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restorations of UISceneSession and AppDelegate is responsible for handling application events.

If you found this helpful give me some claps and follow me on medium.

Thank you for reading. Happy Coding :)

--

--

Amit Kumar
Mac O’Clock

Coder by passion.Swift enthusiast. iOS Developer. Xcode. Open Source Contributor.