Introduction to SceneDelegate Lifecycle methods. What is extracted from AppDelegate
Introduction
In previous articles we have explained the AppDelegate
function and lifecycle methods. Now we will explain what responsibility SceneDelegate
and what responsibilities are extracted from AppDelegate.
Before iOS13 and xcode11, creating a project would result by default to have a file AppDelegate
, UIViewController
, storyboard
and couple more files. In contrast, now you will also see a file SceneDelegate
. AppDelegate
is still created and it is still the main entry point for applications. SceneDelegate
extract from it the responsibility to handle what is shown on screen, manage the lifecycle events of the instance of your apps user interface. It adding the option for multi window support. The trigger for multi window support is in the Info.plist
in Application Scene Manifest
(look at the image on the bottom of the article).
AppDelegate
application (_:didFinishLaunchingWithOptions:)
This method still handles all what we have mentioned in the previous article.
Initialise some 3rd party frameworks, initialise CoreData or any Storage etc.
Only exception is that now creating a window and to initialise the first screen is not the right place any more. It is moved to SceneDelegate
.
application (_:configurationForConnecting:options:)
Retrieves the configuration data for UIKit
to use when creating a new scene. Implement this method if you do not include scene-configuration data in your apps Info.plist
file, or if you want to alter the scene configuration data dynamically.
Runes ones when you start the app. If relaunch the app the windows from the previous run will be restored. If it is not implemented it will fallback to the scene from Info.plist
manifest.
application (_:didDiscardSceneSessions:)
Tells the delegate that the user closed one or more of the apps scenes from the app switcher.
SceneDelegate
With the introduction of the new iPad and multi window support, applications now can have multiple scenes. The naming of methods are similar like in AppDelegate
before iOS13 but responsibilities are slightly changed.
scene (_:willConnectTo:options:)
This method is called when your app creates or restores an instance of your user interface. In most case application have one scene and this will be called ones. This is now the place where to configure our first screen and window, root viewcontroller for the window and makes key window.
sceneWillEnterForeground(_:)
Called when the scene moves from background to active state.
This transition occurs for newly created and connected scenes also.
It is almost the same like applicationWillEnterForeground(_:)
.
sceneDidBecomeActive(_:)
This tells the user that scene became active and ready for user interaction, user events. Same as before the applicationDidBecomeActive(_:)
was.
Use this method to prepare the scene, refresh the interface, start animations, start timers. Also if you use some more secured flow, you want on every event occurrence to check, should you prompt to the user Biometric Login.
sceneWillResignActive(_:)
The scene is just about to resign from active
state. The app is going to background
state. Same use as applicationWillResignActive(_:)
.
This is perfect opportunity to stop or pause ongoing task, disable timers, stop updating the interface. Also think about NSNotificationCenter
Observers, i saw many times that some event triggered UI updates from this state.
sceneDidEnterBackground(_:)
The scene is already in background
state. It is not presented any more.
Use this to free up memory or resources in case of complex screen. This can be like picture editing screen or if you have loaded some big files from disk.
After this moment UIKit
take snapshot from scene and present app switcher. This is the moment when you want to blur sensitise data.
sceneDidDisconnect(_:)
The moment when the scene is removed from the app.
This is last moment for some resource cleanup.
Below is an example code. Colourful emojis are added to the code for easier understanding. This is shorter version as in previous articles most of use cases are already presented and they are similar to this ones.
🔵- represent AppDelegate
Lifecycle methods logs
🌆- represent SceneDelegate
Lifecycle methods logs
🟢- represent UIViewController
Lifecycle method logs
// | |
// AppDelegate.swift | |
// LifecycleScene | |
// | |
// Created by Zolt Varga on 01/09/22. | |
// | |
import UIKit | |
@main | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
override init() { | |
super.init() | |
print("🔵 AppDelegate \(#function)") | |
} | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
print("🔵 AppDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
return true | |
} | |
// MARK: UISceneSession Lifecycle | |
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { | |
print("🔵 AppDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
// Called when a new scene session is being created. | |
// Use this method to select a configuration to create the new scene with. | |
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) | |
} | |
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { | |
print("🔵 AppDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
// Called when the user discards a scene session. | |
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. | |
// Use this method to release any resources that were specific to the discarded scenes, as they will not return. | |
} | |
func applicationDidBecomeActive(_ application: UIApplication) { | |
print("🔵 AppDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
} | |
// MARK: - Debug/Helper | |
extension UIApplication.State { | |
func toString() -> String { | |
switch self { | |
case .active: return "active" | |
case .inactive: return "inactive" | |
case .background: return "background" | |
default: return "none" | |
} | |
} | |
} |
// | |
// SceneDelegate.swift | |
// LifecycleScene | |
// | |
// Created by Zolt Varga on 01/09/22. | |
// | |
import UIKit | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. | |
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene. | |
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
guard let _ = (scene as? UIWindowScene) else { return } | |
} | |
func sceneDidDisconnect(_ scene: UIScene) { | |
// Called as the scene is being released by the system. | |
// This occurs shortly after the scene enters the background, or when its session is discarded. | |
// Release any resources associated with this scene that can be re-created the next time the scene connects. | |
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
func sceneDidBecomeActive(_ scene: UIScene) { | |
// Called when the scene has moved from an inactive state to an active state. | |
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
func sceneWillResignActive(_ scene: UIScene) { | |
// Called when the scene will move from an active state to an inactive state. | |
// This may occur due to temporary interruptions (ex. an incoming phone call). | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
func sceneWillEnterForeground(_ scene: UIScene) { | |
// Called as the scene transitions from the background to the foreground. | |
// Use this method to undo the changes made on entering the background. | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
func sceneDidEnterBackground(_ scene: UIScene) { | |
// Called as the scene transitions from the foreground to the background. | |
// Use this method to save data, release shared resources, and store enough scene-specific state information | |
// to restore the scene back to its current state. | |
print("🌆 SceneDelegate \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
} | |
// | |
// ViewController.swift | |
// LifecycleScene | |
// | |
// Created by Zolt Varga on 01/09/22. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
print("🟢 \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
view.backgroundColor = .red | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
print("🟢 \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
print("🟢 \(#function) State: \(UIApplication.shared.applicationState.toString())") | |
} | |
} |
Here are some log examples on the debug console by running the app.



If you created a project with xcode11 or later you can still opt out from SceneDelegate. You need to delete from Info.plist
the Application Scene Manifest.

Application Scene Manifest deletion
Outro
With this article the Lifecycle series are finished.🙂
Hope that it was helpful and most important to practice on own use cases.
If you got to this point, thanks for reading. 🙂 If you like the content please 👏, share, subscribe, buy a coffee it means to me. If you have some suggestions or questions please feel free to comment.