Initialize your app’s data structures, prepare your app to run, and respond to any launch-time requests from the system.
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
UIApplication 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:
Open your project in Xcode.
Select File > New > New File.
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(_: 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(_: method returns. Performing long-running tasks in that method or your
application(_: 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(_: 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.
The system doesn’t include a key unless your app supports the corresponding feature. For example, the system doesn’t include the
remote 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