Parsing SceneDelegate and AppDelegate in iOS 13

We continue to share our practice and translations of articles that may be useful to a mobile developer. In previous articles, we have discussed testing tools and dependency injection optimization in Android, and now let’s look at one of the innovations in iOS 13 – support for multi-window mode and the separation of AppDelegate (the life cycle and application settings) and SceneDelegate (application display).

From the author: Even before Xcode 11, when creating a new project, you knew that some files were created by default, such as AppDelegate.swift, ViewController.swift, and StoryBoard, as well as a few others. But in Xcode 11, you may have noticed that along with the default files above, a new file is created – SceneDelegate.swift.

At first, you may not understand what this file is, what it is for, and how to use SceneDelegate when developing an application. But let’s try to understand the difference between AppDelegate and SceneDelegate.

The AppDelegate will be responsible for the lifecycle and configuration of the application. The SceneDelegate will be responsible for what is displayed on the screen (Windows or Scenes) and control how your application is displayed.

AppDelegate prior to iOS 13
AppDelegate prior to iOS 13

AppDelegate and Scene Delegate responsibilities

AppDelegate

Even in iOS 13, the AppDelegate is still the main entry point for the app. AppDelegate methods are called for application-level lifecycle events. There are three methods in AppDelegate.swift by default that Apple considers important. Let’s look at them:

  1. func application(_:didFinishLaunchingWithOptions:) -> Bool

  2. func application(_:configurationForConnecting:options:) -> UISceneConfiguration

  3. func application(_:didDiscardSceneSessions:)

This method is called towards the end of the application’s startup, after it has been configured. In previous versions of iOS 13, we could use this method to set up a UIWindow object and assign a ViewController instance to the UIWindow object so that it is displayed on the screen. Starting with iOS 13, if your application has scenes, then the AppDelegate is no longer responsible for processing them and moves to the SceneDelegate.

This method is called every time the application needs to display a new scene or window. This method is not called when the application starts, but only when a new scene or new window needs to be obtained.

This method is called every time the user gets rid of the scene, for example by removing it from the multitasking window, or if it is getting rid of programmatically.

If at the moment when the user gets rid of the scene, the application is not running at the moment, then this method will be called shortly after calling the method (_:didFinishLaunchingwithOptions:) for each discarded scene.

Apart from these methods, the AppDelegate can still handle external services such as push notification registration, location detection, application shutdown, and more.

AppDelegate in iOS 13
AppDelegate in iOS 13

SceneDelegate

As of iOS 13, SceneDelegate takes over some of the functionality of AppDelegate. In particular, the UIWindow from the AppDelegate is now associated with the UIScene in the SceneDelegate. An application can have more than one scene, which mainly handles the interface and content of the application. So, SceneDelegate is responsible for what is displayed on the screen in terms of user interface and data.

SceneDelegate in iOS 13
SceneDelegate in iOS 13

The default methods that can be seen in the SceneDelegate are:

  1. scene(_:willConnectTo:options:)

  2. sceneDidDisconnect(_:)

  3. sceneDidBecomeActive(_:)

  4. sceneWillResignActive(_:)

  5. sceneWillEnterForeground(_:)

  6. sceneDidEnterBackground(_:)

These methods are similar to those used in AppDelegate in iOS 12 and earlier. What each method is intended for can be understood by looking at the names, the difference is only in terminology.

This is the first method called in the UISceneSession life cycle. This method creates a new UIWindow, sets up the root view controller, and makes that window the key window to display.

This method is called when the scene is about to start – for example, when the application is launched for the first time or when transitioning from background to foreground.

This method is called right after the WillEnterForeground method, and here the scene is set up, visible, and ready to use.

These methods are called when the application goes into the background.

This is the most interesting of all methods. When a scene is sent to the background, iOS can completely reset it to free up resources. This does not mean that the application is stopped or not running, just that the scene is disconnected from the session and inactive. iOS may decide to reattach that scene to the session when the user brings that scene to the foreground again. This method can be used to discard those resources that are no longer in use.

Conclusion

The main reason Apple added the UISceneDelegate in iOS 13 was to create a good entry point for multi-window apps. From the theory above, you could understand the AppDelegate and SceneDelegate roles in iOS 13 and their lifecycle events. The AppDelegate is responsible for handling application-level events such as application startup, while the SceneDelegate is responsible for scene lifecycle events such as scene creation, destruction, and restoration of the UISceneSession state.

Thank you for your attention! We hope that the material was useful to you.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *