Sobes.tech
Junior — Middle

What exactly is initialized in the main file when the application starts?

sobes.tech AI

Answer from AI

In the main file of an iOS application (usually AppDelegate.swift or SceneDelegate.swift), key components of the application's lifecycle are initialized at startup. Specifically, it involves:

  • Setting up the application window (UIWindow) and its root view controller (rootViewController), which manages the display of the interface.
  • Configuring initial services, such as initializing network request managers, databases, analytics.
  • Handling launch parameters, for example, if the app is opened via a link or notification.

Example of initializing the window and root view controller in SceneDelegate:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }
    window = UIWindow(windowScene: windowScene)
    window?.rootViewController = UINavigationController(rootViewController: MainViewController())
    window?.makeKeyAndVisible()
}