iOS app lifecycle
date
Jun 30, 2023
slug
ios-app-lifecycle
status
Published
tags
lifecycle
summary
This article explains the iOS app lifecycle and the role of
AppDelegate
and SceneDelegate
. An example of the most frequent interaction with the application is given and the system methods called in this case are analyzed.type
Post
DocumentationBasic conceptsLifecycle exampleapplication(_:willFinishLaunchingWithOptions:)application(_:didFinishLaunchingWithOptions:)applicationWillEnterForeground(_:)applicationDidBecomeActive(_:)Event LoopapplicationWillResignActive(_:)applicationDidEnterBackground(_:)applicationWillTerminate(_:)
Documentation
Basic concepts
The life cycle of an iOS application is contained in the methods described by the
UIApplicationDelegate
, UIWindowSceneDelegate
protocols. These protocols are implemented by default by the AppDelegate
and SceneDelegate
classes.At the same time, the
AppDelegate
contains the @main
attribute, which declares the entry point to the application - the compiler automatically generates code that initializes the AppDelegate
instance and calls the main()
methodInside the
main()
function, a UIApplication
instance will be created - this is your application. It is further provided in all functions provided by the UIApplicationDelegate
and UIWindowSceneDelegate
protocolsBoth
AppDelegate
and SceneDelegate
are responsible for managing the life cycle of the application, but they have different areas of influence on applications:AppDelegate
Responsible for general operations related to the application as a whole. | SceneDelegate
Responsible for managing the life cycle of individual scenes (scene).
The scene is an environment in which the user can interact with the application.
One app can have multiple scenes, like opening an app on different screens, or multi-window on an iPad. |
Contains methods related to major events in the application's lifecycle, such as startup, termination, activation, and deactivation. | Contains methods related to major events in the lifecycle of one particular scene, such as creation, activation, deactivation, and destruction. |
Contains methods related to handling notifications such as external device notifications, location notifications, and other system events. | ㅤ |
Used to set up and initialize global resources such as databases, network connections, DI. | ㅤ |
If your app is for iOS 13 or later and you plan to use scenes, it's recommended to use
SceneDelegate
to manage the life cycle of scenes and AppDelegate
for general scene-independent operations. If you are developing an app for older versions of iOS, you can continue to use only the AppDelegate
.When an application supports scenes, UIKit provides separate lifecycle events for each scene. Remember that the user can create and display multiple scenes as well as hide them individually, so each scene has its own life cycle and they can be in different execution states.
For example, while one scene may be on foreground, another may be in the background or be suspended.
Scene support is an optional feature. To enable basic support, add the
UIApplicationSceneManifest
key to the Info.plist
file and set UIApplicationSupportsMultipleScenes
to a Boolean value that matches the application's usage scenario.
The following figure shows the state transitions for scenes. When a user or system requests a new scene for an application, UIKit creates it and moves it to an unattached state. User-requested scenes quickly move to the foreground where they appear on screen. The scene requested by the system is usually moved to the background so that it can handle the event. For example, the system can fire a scene in the background to handle the "find my location" event. When the user dismisses the application's UI, UIKit moves the associated scene to background and eventually to suspended. UIKit can disable the background or suspended scene at any time to reclaim its resources by reverting that scene to an unattached state.

On iOS 12 and earlier, and applications that don't support scenes, UIKit delivers all lifecycle events to a
UIApplicationDelegate
object. The application delegate manages all application windows, including those that are displayed on separate screens. As a result, changes to the application's state affect the entire user interface of the application, including content on external displays.The following figure shows state transitions involving an
AppDelegate
. Upon startup, the system puts the application in the inactive or background state, depending on whether the user interface appears on the screen. When launched to the foreground, the system automatically switches the application to the active state. After that, the state fluctuates between active and background until the application exits.
Lifecycle example
The following describes the methods that form such a flow -
cold start → minimized application → reopen
Of course, there are other
UIApplicationDelegate
methods that allow you to serve different scenarios (for example, opening a deeplink). But once you understand the basic principle, it will be no problem for you to ensure that your application behaves correctly for any requirement.The yellow circle describes user interaction with the application and device, and depending on the use of scenes, the system will call either the
UIApplicationDelegate
or UIWindowSceneDelegate
methods.
application(_:willFinishLaunchingWithOptions:)
Informs the delegate that the startup process has begun, but the saved state has not been restored.
When this method is called, the application is in the inactive state.
This method (and the corresponding
application(:didFinishLaunchingWithOptions:)
) are used to initialize and prepare the application for launch.The method works:
- After launching the application, loading its storyboard or nib files;
- Until the saved state is restored.
When an application is launched by the system for a specific reason, such as opening a URL or using a quick action on the home screen, various
UIApplicationDelegate
methods can be called to handle this launch.- For example, if the application was launched to open a URL, the system calls the
application(_:open:options:)
method after the application is initialized. ThelaunchOptions
dictionary in this method contains the data associated with the launch reason, allowing you to schedule the appropriate behavior. For example, if the URL is a document that the user wanted to open, you can take steps to prevent the app from restoring its previous state.
- Also, in some cases, when the user uses the home screen shortcut to launch the application, the
application(_:performActionFor:completionHandler:)
method is used. This method allows you to handle the correct launch and take the appropriate actions depending on the quick action.
application(_:didFinishLaunchingWithOptions:)
Notifies the delegate that the startup process is almost complete and the application is almost ready to run.
Use this method (and the corresponding
application(_:willFinishLaunchingWithOptions:)
) to complete application initialization and make any final settings.The method works:
- After restoring the saved state;
- Before presenting the application window and other user interface.
This method represents the last chance to process any keys in the
launchOptions
dictionary.At some point after this method fires, the system calls another application delegate method to put the application in the foreground or background state.
applicationWillEnterForeground(_:)
Tells the delegate that the application is about to enter the foreground. Fires only when the application was previously launched and moved to the background, i.e. it won't work on cold start.
A call to this method is always followed by a call to
applicationDidBecomeActive(_:)
, which transitions the application from inactive to active.If the application is based on scenes, it will be called
sceneWillEnterForeground(_:)
applicationDidBecomeActive(_:)
Notifies the delegate that the application has transitioned to the active state.
If you need to update the UI or resume suspended processes after the application returns from background, you can run the appropriate methods in this method.
An application can enter the suspended state by an incoming call or SMS, or when the user leaves the application and begins transitioning to the background state.
Event Loop
This is a special process, the description of which is beyond the scope of this article. While we can assume that the application is running, running, user events are being processed.
applicationWillResignActive(_:)
Notifies the delegate that the application is about to become inactive.
UIKit calls this method to tell the application that it is about to transition from the foreground to the inactive state. An inactive application continues to run, but does not send incoming events to responders.
If it is needed:
- save data
- stop timers
- pause the game
before locking the screen or before switching to another application, this method is the best place to start the corresponding operations.
An application in the inactive state should do minimal work while it waits to transition to the foreground or background state.
If the application is based on scenes, it will be called
sceneWillResignActive(_:)
An application can enter the suspended state by an incoming call or SMS, or when the user leaves the application and begins transitioning to the background state.
applicationDidEnterBackground(_:)
Tells the delegate that the application is now in the background.
Use if it is needed:
- Release shared resources;
- Disable timers;
- Save UI state information;
- Cancel network requests.
There are approximately five seconds before the application is completed and removed from memory.
Avoid using shared system resources such as contacts database queries.
If the application is based on scenes, it will be called
sceneDidEnterBackground(_:)
applicationWillTerminate(_:)
Tells the delegate when the application is about to terminate.
This method tells the application that it will be completely removed from memory. You should use this method to perform any final application cleanup tasks such as:
- Release of shared resources;
- Saving user data and canceling timers.
If the application is based on scenes, it will be called
sceneDidDisconnect(_:)
and applicationWillTerminate(_:)
There are approximately five seconds before the application is completed and removed from memory.
For applications that support background execution, this method is usually not called when the user exits the application, because in that case the application simply transitions to background. However, it will be called if you manually unload an application from memory from the list of running applications.