Application Life-Cycle for IOS Apps

cagla copuroglu
Orion Innovation techClub
6 min readDec 7, 2021

According to the UIKit’s documentation, to respond to life-cycle events of an app version of iOS 13 and later, use UISceneDelegate objects and earlier versions should use UIApplicationDelegate object.

Limelight Leads | credited on: flickr (Attribution 2.0 Generic (CC BY 2.0)

Brief Terminology

App Life Cycle: When your app is in the foreground or background, respond to system notifications and handle other significant system events.

Scene: A scene is often an instance of your app’s user interface executing on a device.

The five states of an iOS app are the following:

  1. Non-running — The app is not running.
  2. Inactive — The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received.
  3. Active — The app is running in the foreground, and receiving events.
  4. Background — The app is running in the background, and executing code.
  5. Suspended — The app is in the background, but no code is being executed.
UIApplicationDelegate object to responds to life-cycle events.

UIApplicationDelegate

When an app launches, the system calls the UIApplicationMain(_:_:_:_:) function. Among its other tasks, this function creates a singleton UIApplicationobject that you access using shared. This function instantiates the application object from the principal class and instantiates the delegate (if any) from the given class and sets the delegate for the application. It also sets up the main event loop, including the application’s run loop, and begins processing events.

Note: Despite the declared return type, this function never returns.

To make migrating to and from different states easier, the operating system invokes particular methods within the application delegate to manage shared behaviors for your app.

When the launch process is started, the application:willFinishLaunchingWithOptions method is called. This is the first time you’ll be able to run any code in the app.

When the launch procedure is nearly complete, the application:didFinishLaunchingWithOptions method is invoked. This method is called before any of the app’s windows are displayed, so it’s the last chance to prepare the interface and make any last changes.

applicationDidBecomeActive

The application delegate will get a callback notification message via the method applicationDidBecomeActive after the application has been active.

This function is also called if the program returns to an active state after being switched to inactive like a phone call or SMS.

applicationWillResignActive

The applicationWillResignActive function is triggered by a number of situations. This method is invoked whenever a transitory event occurs, such as a phone call. It’s also worth noting that “quitting” an iOS program does not kill processes; rather, it puts the app in the background.

applicationDidEnterBackground

When an iOS app is running but no longer in the foreground, this method is invoked. To put it another way, the user interface is now hidden. The app has around five seconds to complete duties and return, according to Apple’s UIApplicationDelegate Protocol Reference. The program crashes if the method does not return within five seconds.

It’s worth noting, though, that the operating system places restrictions on what an app may perform in the background. You must seek permission if a program needs to run in the background (with restricted functionality).

applicationWillEnterForeground

When an app prepares to transition from the background to the foreground, this function is called. The app, on the other hand, does not become active until the applicationDidBecomeActive method is invoked. Before the program becomes active, this approach allows a developer to re-establish the settings of the prior operating state.

applicationWillTerminate

When a termination event occurs, this method tells your application delegate. The app no longer closes when you press the home button. The applicationWillTerminate method is called when the iOS app is forced to shut down or the device is turned off. This is where you may keep the application’s settings, configuration, and user preferences.

App Life Cycle

SceneDelegate

In iOS 13 or later, UISceneDelegate provides the following core methods to respond to life-cycle events happening within a scene.

Connecting and Disconnecting the Scene:

func scene(UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions)

Tells the delegate about the addition of a scene to the app.

When your program creates or restores a user interface object, this method is invoked.

UIKit produces an appropriate scene object and attaches it to your app when the user or your app requests a new instance of your user interface. This function is used to respond to the insertion of a new scene and to start loading any data that the scene requires.

func sceneDidDisconnect(UIScene)

Notifies the delegate that a scene in your app was deleted by UIKit. Before your scene is purged from memory, use this technique to do any last cleanup. Use it to preserve user data and release references to files or shared resources, for example.

The elimination of a scene is a sign that it is about to be destroyed. When a user closes a scene in the app switcher, UIKit disconnects it. In order to free up memory for other processes, UIKit may detach a scene. When the user changes to another app, UIKit does not instantly detach the scene.

Transitioning to the Foreground

func sceneWillEnterForeground(UIScene)

Tells the delegate that the scene is about to begin running in the foreground and become visible to the user.

Before bringing a scene to the foreground, UIKit invokes this function. This transition occurs for scenes that are freshly produced and linked, as well as scenes that were previously operating in the background and were brought to the foreground by the system or a user action. This method is always followed by a call to the sceneDidBecomeActive(_:) function because a scene reaches the foreground before being visible onscreen.

func sceneDidBecomeActive(UIScene)

Tells the delegate that the scene became active and is now responding to user events.

Use this way to get your scenario ready for the screen. This function is called by UIKit after your scene’s interface has been loaded but before it displays onscreen. It may be used to refresh the contents of views, initiate timers, or raise the frame rate of your user interface.

Transitioning to the Background

func sceneWillResignActive(UIScene)

Tells the delegate that the scene is about to resign the active state and stop responding to user events.

This function is used by UIKit to handle brief disruptions, such as when showing system alarms. Before switching your program to the background state, it also calls the method.

This technique may be used to silence your interface and get it ready to quit communicating with the user. Pause running processes, turn off timers, lower frame rates, or turn off refreshing your interface entirely. This technique should be used to pause games. Your app should be doing minimal work by the time this method returns, since it waits to transition to the background or back to the foreground.

If your scene contains unsaved user data, save it using this technique to prevent it from being lost. However, you should never rely on this strategy to preserve data. Instead, save it from your view controllers at suitable moments, generally in reaction to user events. When a user closes a data-entry screen, for example, save the data. Don’t rely on app transitions to store all of your app’s important data.

func sceneDidEnterBackground(UIScene)

Tells the delegate that the scene is running in the background and is no longer on screen.

This approach may be used to minimize memory utilization, free up shared resources, and tidy up the user interface of your scene. UIKit takes a snapshot of your scene’s interface for display in the app switcher shortly after this function returns. Make sure your user interface doesn’t include any sensitive data.

In conclusion it’s safe to say that developers can use methods provided from either UIApplicationDelegate or SceneDelegate to manage the life-cycle of their apps.

--

--