App Life Cycle

Janvi Arora
4 min readFeb 28, 2023

--

AppDelegate Methods:

1. func application(_:didFinishLaunchingWithOptions:) -> Bool
2. func application(_:configurationForConnecting:options:) -> UISceneConfiguration
3. func application(_:didDiscardSceneSessions:)
  • func application(_:didFinishLaunchingWithOptions:) -> Bool

This method is called when the application is launched. The application set-up is done here. It tells the delegate that the launch process is almost done, and the app is almost ready to run.

  • func application(_:configurationForConnecting:options:) -> UISceneConfiguration

This method is called whenever the application needs a new scene or window to display. This method is not called on app launch, but only when a new scene or a new window needs to be obtained.

  • func application(_:didDiscardSceneSessions:)

This method is called whenever the user discards a scene by swiping it from the multitasking window, or if discarded programmatically. This method is called for every discarded scene shortly after the (_:didFinishLaunchingwithOptions:) method is called if the app isn’t running when the user discards the scene.

Key points:

  1. AppDelegate can also handle external services like push notification registrations, location services, app termination and more.

2. Why (_:didFinishLaunchingwithOptions:) method is always true? — The didFinishLaunchingWithOptions method is not always true by default. It returns a boolean value indicating whether the launch was successful or not, and it's up to the app developer to implement this method in a way that returns the appropriate value based on the app's needs.

Typically, the didFinishLaunchingWithOptions method is used to perform any initial setup and configuration required for the app to run, and then return true to indicate that the app launch was successful. If there are any errors or issues during this process, the method can return false to indicate that the launch was not successful and the app should not continue running.

However, it’s worth noting that some Xcode project templates may include a default implementation of the didFinishLaunchingWithOptions method that always returns true, which is why it may appear as though the method is always true. In these cases, it's up to the app developer to modify the implementation of the method as needed to ensure that it returns the appropriate value for their specific app requirements.

3. What happens if we make (_:didFinishLaunchingwithOptions:) method false? — If you make the application(_:didFinishLaunchingWithOptions:) method return false, it will indicate that the launch process failed and the app should be terminated. In this case, the app will not be launched and the user will not see the app's UI. Instead, the app will terminate immediately after launching.

Typically, you should only return false if there is an unrecoverable error that prevents your app from launching properly.

// This makes us able to print the function name as and when called.
print(#function)
To enable multiple screens, make “Enable Multiple Windows” as “YES”
iOS 13 methods after division (Because in earlier iOS versions, all methods were handled by AppDelegate only. There was no SceneDelegate available)
Launch and termination methods

SceneDelegate Methods:

1. scene(_:willConnectTo:options:)
2. sceneDidDisconnect(_:)
3. sceneDidBecomeActive(_:)
4. sceneWillResignActive(_:)
5. sceneWillEnterForeground(_:)
6. sceneDidEnterBackground(_:)
  • scene(_:willConnectTo:options:)

This is the first method that is called in the UISceneSession life cycle. This method will create a new UIWindow, sets the root view controller and makes this window the key window to be displayed.

  • sceneWillEnterForeground(_:)

This method is called when the scene is about to start, like when the app becomes active for the first time or when transitions from background to foreground.

  • sceneDidBecomeActive(_:)

This method is called right after the sceneWillEnterForeground(_:) method, and here the scene is set up and visible and ready to use.

  • sceneWillResignActive(_:) and sceneDidEnterBackground(_:)

These methods are called when the app stages to the background.

  • sceneDidDisconnect(_:)

Whenever the scene is sent to the background, iOS might decide to completely discard the scene to free up the resources. This doesn’t mean that the app is killed or not running, but just the scene is disconnected from the session and is not active. iOS can decide to reconnect back this scene to the scene session when the user brings that particular scene to the foreground again. This method can be used to discard any resources that aren’t used anymore.

Why UISceneDelegate was added to iOS 13?
- To create a good entry for multi-windowes applications.

— AppDelegate is responsible for handling application-level events, like app launch

— SceneDelegate is responsible for scene lifecycle events like scene creation, destruction, and state restoration of a UISceneSession.

Programmatically setting up of app with SceneDelegate (steps):

  1. Remove Main.storyboard file.
  2. Remove 2 key-value pairs in info.plist file, containing “Main” as their value.
  3. Create your initial Viewcontroller or use the default ViewController.
  4. Programmatically create your view hierarchy.
// Steps involved in creation of view hierarchy  
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

// 1. Capture the scene
guard let windowScene = (scene as? UIWindowScene) else { return }

// 2. Create a new UIWindow using the windowScene constructor which takes in a window scene.
let window = UIWindow(windowScene: windowScene)

// 3. Create a view hierarchy programmatically
let viewController = AViewController()
let navigation = UINavigationController(rootViewController: viewController)

// 4. Set the root view controller of the window with your view controller
window.rootViewController = navigation

// 5. Set the window and call makeKeyAndVisible()
self.window = window
window.makeKeyAndVisible()
}

--

--