Hello everyone! I’m excited to share my knowledge about the Application Life Cycle and how it is organized in Swift.

Often beginners create an app and don’t think about the application life cycle but the application life cycle is very important to understand.
The current state of your app determines what it can and can’t do at any time. For example, a foreground app has the user’s attention, so it has priority over system resources, including the CPU. By contrast, a background app must do as little work as possible, and preferably nothing, because it’s offscreen. As your app changes from state to state, you must adjust its behavior accordingly.
When your app’s state changes, UIKit notifies you by calling methods of the appropriate delegate object:
- In iOS 13 and later, use
UISceneDelegateobjects to respond to life-cycle events in a scene-based app. - In iOS 12 and earlier, use the
UIApplicationDelegateobject to respond to life-cycle events.
Let’s fully understand the steps that our app goes through until it is displayed on the screen and users can interact 🔥
If you don’t mind, let’s start from scratch 🥶
- The user turns on the iOS device.
- The iOS operating system boots up.
- The home screen (SpringBoard) is displayed.
- The user taps on our app’s icon on the home screen.
- SpringBoard launches our app by initiating a launch event for it.
- After SpringBoard, our app passes through many states before being displayed on the screen. We will talk about that a little bit later.
Above I briefly introduced what happens when you tap any application to turn on.
SpringBoard is the name of the application that manages the home screen on iOS devices. It’s essentially the user interface that you see when you unlock your iOS device, displaying the icons for all your installed apps and allowing you to launch them. SpringBoard also manages the device’s wallpaper, handles app launching and switching, and provides the foundation for the user interface experience on iOS. It’s a core part of the iOS operating system that ensures a smooth and consistent user experience.
Let’s take a look at execution states for apps 🚀
- Not Running state: The app has not been launched or terminated the app by the system.
- Inactive state: The app is entering the foreground state but cannot receive events or handle user interaction.
- Active state: The app is in the active state when it’s running and responding to user interactions. This is the state where the app performs its primary tasks, processes user input, and updates the user interface.
- Background state: When the user presses the home button or switches to another app, the app enters the background state. In this state, the app is still running but is no longer in the foreground. Background tasks, such as playing music or downloading data, can continue. However, apps are given limited resources in this state to conserve battery life and system performance. Also, if there is no executable code or the execution is complete, the application will be suspended immediately.
- Suspended State: If the system needs to free up resources, it may move the app to the suspended state. In this state, the app is not actively running code. It’s still in memory but not executing. The app can be terminated by the system if it remains in this state for an extended period.
- Terminated State: The app can be terminated by the user, the system, or due to an error. When an app is terminated, it is removed from memory, and its resources are released. The next launch of the app starts the lifecycle over again.
# Key Events and Methods in the iOS Application Lifecycle:
- application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:) -> Bool: This method is called when the app is launched, allowing you to perform setup tasks.
- sceneWillEnterForeground(_ scene: UIScene): Called as the scene transitions from the background to the foreground.
- sceneDidBecomeActive(_ scene: UIScene): Called when the scene has moved from an inactive state to an active state.
- sceneWillResignActive(_ scene: UIScene): Called when the scene will move from an active state to an inactive state.
- sceneDidEnterBackground(_ scene: UIScene): Called as the scene transitions from the foreground to the background.
- applicationWillTerminate(_ application: UIApplication): This method is called when the app is terminated.

The above image illustrates how app states interact with each other. Also, below, I’ve provided code that you can copy and paste to check the app’s state.
# AppDelegate
import UIKit
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
print("didFinishLaunchingWithOptions")
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// 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>) {
// 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 applicationWillTerminate(_ application: UIApplication) {
print("My App is Termiated")
}
}# SceneDelegate
import UIKit
final 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).
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
print("sceneDidDisconnect")
// 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).
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("sceneDidBecomeActive")
// 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.
}
func sceneWillResignActive(_ scene: UIScene) {
print("sceneWillResignActive")
// 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).
}
func sceneWillEnterForeground(_ scene: UIScene) {
print("sceneWillEnterForeground")
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
print("sceneDidEnterBackground")
// 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.
}
}
No comments:
Post a Comment