Article

Responding to the Launch of Your App

Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.

Overview

The system launches your app when the user taps your app’s icon on the Home screen. If your app requested specific events, the system might also launch your app in the background to handle those events. For a scene-based app, the system similarly launches the app when one of your scenes needs to appear onscreen or do some work.

All apps have an associated process, represented by the UIApplication object. Apps also have an app delegate object—an object that conforms to the UIApplicationDelegate protocol—whose job is to respond to important events happening within that process. Even a scene-based app uses an app delegate to manage fundamental events like launch and termination. At launch time, UIKit automatically creates the UIApplication object and your app delegate. It then starts your app’s main event loop.

Provide a Launch Storyboard

When the user first launches your app on a device, the system displays your launch storyboard until your app is ready to display its UI. Displaying the launch storyboard assures the user that your app launched and is doing something. If your app initializes itself and readies its UI quickly, the user may see your launch storyboard only briefly.

Xcode projects automatically include a default launch storyboard for you to customize, and you can add more launch storyboards as needed. To add new launch storyboards to your project, do the following:

  1. Open your project in Xcode.

  2. Select File > New > New File.

  3. Add a Launch Screen resource to your project.

Add views to your launch storyboard, and use Auto Layout constraints to size and position those views so that they adapt to the underlying environment. For example, you might create a splash screen with a solid-color background and a fixed-size image of your company logo or other content. Always create a final version of the interface you want. Never include temporary content or content that requires localization. UIKit displays exactly what you provide, using your constraints to fit your views into the available space.

Initialize Your App's Data Structures

Put your app's launch-time initialization code in one or both of the following methods:

UIKit calls these methods at the beginning of your app’s launch cycle. Use them to:

  • Initialize your app's data structures.

  • Verify that your app has the resources it needs to run.

  • Perform any one-time setup when your app is launched for the first time. For example, install templates or user-modifiable files in a writable directory; see Performing One-Time Setup for Your App.

  • Connect to any critical services that your app uses. For example, connect to the Apple Push Notification service if your app supports remote notifications.

  • Check the launch options dictionary for information about why your app was launched; see Determine Why Your App Was Launched.

For apps that aren't scene-based, UIKit loads your default user interface automatically at launch time. Use the application:didFinishLaunchingWithOptions: method to make additional changes to that interface before it appears onscreen. For example, you might install a different view controller to reflect what the user was doing the last time they used the app.

Move Long-Running Tasks off the Main Thread

When the user launches your app, make a good impression by launching quickly. UIKit doesn't present your app's interface until after the application:didFinishLaunchingWithOptions: method returns. Performing long-running tasks in that method or your application:willFinishLaunchingWithOptions: method might make your app appear sluggish to the user. Returning quickly is also important when launching to the background, because your app’s background execution time is limited.

Move tasks that are not critical to your app’s initialization out of the launch-time sequence. For example:

  • Defer the initialization of features that are not needed immediately.

  • Move important, long-running tasks off your app’s main thread. For example, run them asynchronously on a global dispatch queue.

Determine Why Your App Was Launched

When UIKit launches your app, it passes along a launch options dictionary to your application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods with information about why your app was launched. The keys in that dictionary indicate important tasks to perform immediately. For example, they might reflect actions that the user started elsewhere and want to continue in your app. Always check the contents of the launch options dictionary for keys that you expect, and respond appropriately to their presence.

Listing 1 shows the app delegate method for an app that handles background location updates. When the location key is present, the app starts location updates immediately, instead of deferring them until later. Starting location updates allows the Core Location framework to deliver the new location event.

Listing 1

Responding to a location event at launch time

class AppDelegate: UIResponder, UIApplicationDelegate, 
               CLLocationManagerDelegate {
    
   let locationManager = CLLocationManager()
   func application(_ application: UIApplication,
              didFinishLaunchingWithOptions launchOptions:
              [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
      // If launched because of new location data,
      //  start the visits service right away.
      if let keys = launchOptions?.keys {
         if keys.contains(.location) {
            locationManager.delegate = self
            locationManager.startMonitoringVisits()
         }
      }
       
      return true
   }
   // other methods…
}

The system doesn’t include a key unless your app supports the corresponding feature. For example, the system doesn’t include the UIApplicationLaunchOptionsRemoteNotificationKey key for an app that doesn’t support remote notifications.

For a list of launch option keys and information about how to handle them, see UIApplicationLaunchOptionsKey.

Topics

Launch Time

About the App Launch Sequence

Learn the order in which your custom code is executed at launch time.

Performing One-Time Setup for Your App

Ensure that your app environment is properly configured.

Preserving Your App's UI Across Launches

Return your app to its previous state after it is terminated by the system.

See Also

Life Cycle

Managing Your App's Life Cycle

Respond to system notifications when your app is in the foreground or background, and handle other significant system-related events.

UIApplication

The centralized point of control and coordination for apps running in iOS.

UIApplicationDelegate

A set of methods that you use to manage shared behaviors for your app.

Scenes

Manage multiple instances of your app’s UI simultaneously, and direct resources to the appropriate instance of your UI.