Protocol

UIApplicationDelegate

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

Declaration

protocol UIApplicationDelegate

Overview

Your app delegate object manages your app’s shared behaviors. The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app’s launch cycle so it is always present.

Use your app delegate object to handle the following tasks:

  • Initializing your app’s central data structures.

  • Configuring your app’s scenes.

  • Responding to notifications originating from outside the app, such as low-memory warnings, download completion notifications, and more.

  • Responding to events that target the app itself, and are not specific to your app’s scenes, views, or view controllers.

  • Registering for any required services at launch time, such as Apple Push Notification service.

For more information about how you use the app delegate object to initialize your app at launch time, see Responding to the Launch of Your App.

Life Cycle Management in iOS 12 and Earlier

In iOS 12 and earlier, you use your app delegate to manage major life cycle events in your app. Specifically, you use methods of the app delegate to update the state of your app when it enters the foreground or moves to the background.

Topics

Initializing the App

func application(UIApplication, willFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool

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

func application(UIApplication, didFinishLaunchingWithOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool

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

struct UIApplication.LaunchOptionsKey

Keys used to access values in the launch options dictionary passed to your app at initialization time.

func applicationDidFinishLaunching(UIApplication)

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

class let didFinishLaunchingNotification: NSNotification.Name

Posted immediately after the app finishes launching.

Configuring and Discarding Scenes

func application(UIApplication, didDiscardSceneSessions: Set<UISceneSession>)

Tells the delegate that the user closed one or more of the app's scenes from the app switcher.

Beta

Responding to App Life-Cycle Events

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 applicationDidEnterBackground(UIApplication)

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

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.

class let didEnterBackgroundNotification: NSNotification.Name

Posted when the app enters the background.

class let willEnterForegroundNotification: NSNotification.Name

Posted shortly before an app leaves the background state on its way to becoming the active app.

class let willResignActiveNotification: NSNotification.Name

Posted when the app is no longer active and loses focus.

class let willTerminateNotification: NSNotification.Name

Posted when the app is about to terminate.

Responding to Environment Changes

func applicationProtectedDataDidBecomeAvailable(UIApplication)

Tells the delegate that protected files are available now.

func applicationProtectedDataWillBecomeUnavailable(UIApplication)

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

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.

class let protectedDataDidBecomeAvailableNotification: NSNotification.Name

Posted when the protected files become available for your code to access.

class let protectedDataWillBecomeUnavailableNotification: NSNotification.Name

Posted shortly before protected files are locked down and become inaccessible.

class let didReceiveMemoryWarningNotification: NSNotification.Name

Posted when the app receives a warning from the operating system about low memory availability.

class let significantTimeChangeNotification: NSNotification.Name

Posted when there is a significant change in time, for example, change to a new day (midnight), carrier time update, and change to or from daylight savings time.

Managing App State Restoration

func application(UIApplication, shouldSaveApplicationState: NSCoder) -> Bool

Asks the delegate whether the app’s state should be preserved.

func application(UIApplication, shouldRestoreApplicationState: NSCoder) -> Bool

Asks the delegate whether the app’s saved state information should be restored.

func application(UIApplication, willEncodeRestorableStateWith: NSCoder)

Tells your delegate to save any high-level state information at the beginning of the state preservation process.

func application(UIApplication, didDecodeRestorableStateWith: NSCoder)

Tells your delegate to restore any high-level state information as part of the state restoration process.

class let stateRestorationBundleVersionKey: String

The version of your app responsible for creating the restoration archive.

class let stateRestorationSystemVersionKey: String

The version of the system on which your app created the restoration archive.

class let stateRestorationTimestampKey: String

The time at which your app created the restoration archive.

class let stateRestorationUserInterfaceIdiomKey: String

The user interface idiom that was in effect when your app created the restoration archive.

class let stateRestorationViewControllerStoryboardKey: String

A reference to the storyboard that contains the view controller.

Downloading Data in the Background

func application(UIApplication, performFetchWithCompletionHandler: (UIBackgroundFetchResult) -> Void)

Tells the app that it can begin a fetch operation if it has data to download.

Deprecated
func application(UIApplication, handleEventsForBackgroundURLSession: String, completionHandler: () -> Void)

Tells the delegate that events related to a URL session are waiting to be processed.

enum UIBackgroundFetchResult

Constants that indicate the result of a background fetch operation.

Handling Remote Notification Registration

func application(UIApplication, didRegisterForRemoteNotificationsWithDeviceToken: Data)

Tells the delegate that the app successfully registered with Apple Push Notification service (APNs).

func application(UIApplication, didFailToRegisterForRemoteNotificationsWithError: Error)

Sent to the delegate when Apple Push Notification service cannot successfully complete the registration process.

Continuing User Activity and Handling Quick Actions

func application(UIApplication, willContinueUserActivityWithType: String) -> Bool

Tells the delegate if your app takes responsibility for notifying users when a continuation activity takes longer than expected.

func application(UIApplication, didUpdate: NSUserActivity)

Tells the delegate that the activity was updated.

func application(UIApplication, performActionFor: UIApplicationShortcutItem, completionHandler: (Bool) -> Void)

Called when the user selects a Home screen quick action for your app, except when you’ve intercepted the interaction in a launch method.

Interacting With WatchKit

Interacting With HealthKit

func applicationShouldRequestHealthAuthorization(UIApplication)

Called when your app should ask the user for access to his or her HealthKit data.

Opening a URL-Specified Resource

func application(UIApplication, open: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool

Asks the delegate to open a resource specified by a URL, and provides a dictionary of launch options.

struct UIApplication.OpenURLOptionsKey

Keys used to access values in the options dictionary when opening a URL.

Disallowing Specified App Extension Types

func application(UIApplication, shouldAllowExtensionPointIdentifier: UIApplication.ExtensionPointIdentifier) -> Bool

Asks the delegate to grant permission to use app extensions that are based on a specified extension point identifier.

Handling SiriKit Intents

func application(UIApplication, handle: INIntent, completionHandler: (INIntentResponse) -> Void)

Asks the delegate to handle the specified SiriKit intent directly.

Handling CloudKit Invitations

func application(UIApplication, userDidAcceptCloudKitShareWith: CKShareMetadata)

Tells the delegate that the app now has access to shared information in CloudKit.

Managing Interface Geometry

func application(UIApplication, supportedInterfaceOrientationsFor: UIWindow?) -> UIInterfaceOrientationMask

Asks the delegate for the interface orientations to use for the view controllers in the specified window.

func application(UIApplication, willChangeStatusBarOrientation: UIInterfaceOrientation, duration: TimeInterval)

Tells the delegate when the interface orientation of the status bar is about to change.

Deprecated
func application(UIApplication, didChangeStatusBarOrientation: UIInterfaceOrientation)

Tells the delegate when the interface orientation of the status bar has changed.

Deprecated
func application(UIApplication, willChangeStatusBarFrame: CGRect)

Tells the delegate when the frame of the status bar is about to change.

Deprecated
func application(UIApplication, didChangeStatusBarFrame: CGRect)

Tells the delegate when the frame of the status bar has changed.

Deprecated
enum UIInterfaceOrientation

The orientation of the app's user interface.

struct UIInterfaceOrientationMask

These constants are mask bits for specifying a view controller’s supported interface orientations.

class let invalidInterfaceOrientationException: NSExceptionName

This exception is thrown if a view controller or the app returns 0 instead of a valid set of supported interface orientation values. It is also thrown if the orientation returned by a view controller’s preferredInterfaceOrientationForPresentation method does not match one of the view controller’s supported orientations.

Providing a Window for Storyboarding

var window: UIWindow?

The window to use when presenting a storyboard.

Deprecated Symbols

func application(UIApplication, didRegister: UIUserNotificationSettings)

Called to tell the delegate the types of local and remote notifications that can be used to get the user’s attention.

Deprecated
func application(UIApplication, didReceive: UILocalNotification)

Sent to the delegate when a running app receives a local notification.

Deprecated
func application(UIApplication, didReceiveRemoteNotification: [AnyHashable : Any])

Called when your app has received a remote notification.

Deprecated
func application(UIApplication, handleActionWithIdentifier: String?, for: UILocalNotification, completionHandler: () -> Void)

Called when your app has been activated because user selected a custom action from the alert panel of a local notification.

Deprecated
func application(UIApplication, handleActionWithIdentifier: String?, forRemoteNotification: [AnyHashable : Any], completionHandler: () -> Void)

Tells the app delegate to perform the custom action specified by a remote notification.

Deprecated
func application(UIApplication, handleOpen: URL) -> Bool

Asks the delegate to open a resource identified by URL.

Deprecated
func application(UIApplication, open: URL, sourceApplication: String?, annotation: Any) -> Bool

Asks the delegate to open a resource identified by a URL.

Deprecated

Relationships

Inherits From

See Also

Application

class UIApplication

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software