About the background execution sequence
Learn the order in which your custom code is executed when your app moves to the background.
Overview
An app may enter the background from one of several different starting points. System events can cause a suspended app to be returned to the background, or cause a not running app to be launched directly into the background. A foreground app transitions to the background when another app is launched or when the user returns to the Home screen.
[Image]
Handle background events
For apps that support one of the Background Modes capabilities, the system launches or resumes the app in the background to handle events associated with those capabilities. For example, the system might launch or resume the app to respond to a location update or to perform a background fetch.
[Image]
If your app isn’t running when an event arrives, the system launches the app and moves it directly to the background, following this sequence:
The system launches the app and follows the initialization sequence described in About the app launch sequence.
UIKit calls the app delegate’s applicationDidEnterBackground(_:) method.
UIKit delivers the event that caused the launch.
The app’s snapshot is taken.
The app may be suspended again.
If your app is in memory and suspended when an event arrives, the system resumes the app in the background, following this sequence:
The system resumes the app.
UIKit calls the app delegate’s applicationDidEnterBackground(_:) method.
UIKit delivers the event that caused the launch.
The app’s snapshot is taken.
The app may be suspended again.
Transition from the foreground
When another app is launched or the user returns to the Home screen, the foreground app moves to the background, following this sequence:
The user exits the running app.
UIKit calls the app delegate’s applicationWillResignActive(_:) method.
UIKit calls the app delegate’s applicationDidEnterBackground(_:) method.
The app’s snapshot is taken.
The app may be suspended again.