Instance Method

applicationDidEnterBackground(_:)

Tells the delegate that the app is now in the background.

Declaration

optional func applicationDidEnterBackground(_ application: UIApplication)

Parameters

application

Your singleton app object.

Discussion

Use this method to release shared resources, invalidate timers, and store enough app state information to restore your app to its current state in case it is terminated later. You should also disable updates to your app’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTask(expirationHandler:). In practice, you should return from applicationDidEnterBackground(_:) as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground(_:) will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTask(expirationHandler:) and then run the task on a dispatch queue or secondary thread.

The app also posts a UIApplicationDidEnterBackground notification around the same time it calls this method to give interested objects a chance to respond to the transition.

For more information about how to transition gracefully to the background, and for information about how to start background tasks, see App Programming Guide for iOS.

See Also

Responding to App State Changes and System Events

func application(UIApplication, willFinishLaunchingWithOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)

Tells the delegate that the launch process has begun but that state restoration has not yet occurred.

func application(UIApplication, didFinishLaunchingWithOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)

Tells the delegate that the launch process is almost done and the app is almost ready to run.

func applicationDidFinishLaunching(UIApplication)

Tells the delegate when the app has finished launching. Don’t use. Instead, use application(_:didFinishLaunchingWithOptions:).

func applicationDidBecomeActive(UIApplication)

Tells the delegate that the app has become active.

func applicationWillResignActive(UIApplication)

Tells the delegate that the app is about to become inactive.

func applicationWillEnterForeground(UIApplication)

Tells the delegate that the app is about to enter the foreground.

func applicationWillTerminate(UIApplication)

Tells the delegate when the app is about to terminate.

func applicationProtectedDataWillBecomeUnavailable(UIApplication)

Tells the delegate that the protected files are about to become unavailable.

func applicationProtectedDataDidBecomeAvailable(UIApplication)

Tells the delegate that protected files are available now.

func applicationDidReceiveMemoryWarning(UIApplication)

Tells the delegate when the app receives a memory warning from the system.

func applicationSignificantTimeChange(UIApplication)

Tells the delegate when there is a significant change in the time.