The UIApplication class provides a centralized point of control and coordination for applications running on iPhone OS.
Every application must have exactly one instance of UIApplication (or a subclass of UIApplication). When an application is launched, the UIApplicationMain function is called; among its other tasks, this function create a singleton UIApplication object. Thereafter you can access this object by invoking the sharedApplication class method.
A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches action messages forwarded to it by control objects (UIControl) to the appropriate target objects. In addition, the UIApplication object maintains a list of all the windows (UIWindow objects) currently open in the application, so through those it can retrieve any of the application’s UIView objects. The application object is typically assigned a delegate, an object that the application informs of significant runtime events for example, application launch, low-memory warnings, and application termination—giving it an opportunity to respond appropriately.
Applications can cooperatively handle a resource such as an email or an image file through the openURL: method. For example, an application opening an email URL with this method may cause the mail client to launch and display the message.
The programmatic interfaces of UIApplication and UIApplicationDelegate also allow you to manage behavior that is specific to the device. You can control application response to changes in interface orientation, temporarily suspend incoming touch events, and turn proximity sensing (of the user’s face) off and on again.
UIApplication defines a delegate that must adopt the UIApplicationDelegate protocol and implement some of the protocol methods.
You might decide to subclass UIApplication to override sendEvent: or sendAction:to:from:forEvent: to implement custom event and action dispatching. However, there is rarely a valid need to extend this class; the application delegate (UIApplicationDelegate is sufficient for most occasions.
The UIApplicationDelegate protocol declares methods that are implemented by the delegate of the singleton UIApplication object. These methods provide you with information about key events in an application’s execution such as when it finished launching, when it is about to be terminated, when memory is low, and when important changes occur. Implementing these methods gives you a chance to respond to these system events and respond appropriately.
Launch time is also a particularly important point in an application’s life cycle. In addition to the user launching an application by tapping its icon, an application can be launched in order to respond to a specific type of event. For example, it could be launched in response to an incoming push notification, it could be asked to open a file, or it could be launched to handle some background event that it had requested. In all of these cases, the options dictionary passed to the application:didFinishLaunchingWithOptions: method provides information about the reason for the launch.
By implementing these methods, the delegate can respond to application launch and termination, low-memory warnings, the opening of URL resources, changes in status-bar orientation, and other system events. Tasks Opening a URL Resource
* – application:handleOpenURL: optional method
Managing Status Bar Orientation
* – application:willChangeStatusBarOrientation:duration: optional method
* – application:didChangeStatusBarOrientation: optional method
Responding to a Change in Active Status
* – applicationWillResignActive: optional method
* – applicationDidBecomeActive: optional method
Controlling Application Appearance
* – application:willChangeStatusBarFrame: optional method
* – application:didChangeStatusBarFrame: optional method
Controlling Application Behavior
* – applicationDidFinishLaunching: optional method
* – applicationWillTerminate: optional method
* – applicationDidReceiveMemoryWarning: optional method
* – applicationSignificantTimeChange: optional method
Instance Methods application:didChangeStatusBarFrame:
Tells the delegate when the frame of the status bar has changed. This method is optional.
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame Parameters
application
The delegating application object.
oldStatusBarFrame
The previous frame of the status bar, in screen coordinates.
Discussion
The application post a UIApplicationDidChangeStatusBarFrameNotification at the same time it calls this method. Availability
- Available in iPhone OS 2.0 and later.
See Also
- – application:willChangeStatusBarFrame:
Declared In UIApplication.h application:didChangeStatusBarOrientation:
Tells the delegate when the interface orientation of the status bar has changed. This method is optional.
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation Parameters
application
The delegating application object.
oldStatusBarOrientation
A constant that indicates the previous orientation of the application’s user interface; see “Controlling Application Behavior” for details.
Discussion
The delegate can get the current device orientation from the shared UIDevice object.
The application posts a UIApplicationDidChangeInterfaceOrientationNotification at the same time it sends this message to its delegate. Availability
- Available in iPhone OS 2.0 and later.
Declared In UIApplication.h application:handleOpenURL:
Asks the delegate to open a resource identified by URL. This method is optional.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url Parameters
application
The application object.
url
A object representing a URL (Universal Resource Locator). See the appendix of iPhone Application Programming Guide for Apple-registered schemes for URLs.
Return Value
YES if the delegate successfully handle the request; NO if the attempt to handle the URL failed. Discussion
There is no equivalent notification for this delegation method. Availability
- Available in iPhone OS 2.0 and later.
See Also
- – openURL: (UIApplication)
Declared In UIApplication.h application:willChangeStatusBarFrame:
Tells the delegate when the frame of the status bar is about to change. This method is optional.
- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame Parameters
application
The delegating application object.
newStatusBarFrame
The changed frame of the status bar, in screen coordinates.
Discussion
The application calls this method when it receives a setStatusBarOrientation:animated: message and is about to change the interface orientation.
The application posts a UIApplicationWillChangeStatusBarFrameNotification at the same time it calls this method. Availability
- Available in iPhone OS 2.0 and later.
See Also
- – application:didChangeStatusBarFrame:
Declared In UIApplication.h application:willChangeStatusBarOrientation:duration:
Tells the delegate when the interface orientation of the status bar is about to change. This method is optional.
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration Parameters
application
The delegating application object.
newStatusBarOrientation
A constant that indicates the new orientation of the application’s user interface; see “Controlling Application Behavior” for details.
duration
The duration of the animation to the new orientation, in seconds.
Discussion
The delegate typically implements this method to prepare its windows and views for the new orientation. The delegate can get the current device orientation from the shared UIDevice object.
The application posts a UIApplicationWillChangeInterfaceOrientationNotification at the same time it sends this message to its delegate. Availability
- Available in iPhone OS 2.0 and later.
Declared In UIApplication.h applicationDidBecomeActive:
Tells the delegate that the application has become active. This method is optional.
- (void)applicationDidBecomeActive:(UIApplication *)application Parameters
application
The singleton application instance.
Discussion
The delegate can implement this method to make adjustments when the application transitions from an inactive state to an active state. When an application is inactive, it is executing but is not dispatching incoming events. This occurs when an overlay window pops up or when the device is locked.
Just after it becomes active, the application also posts a UIApplicationDidBecomeActiveNotification. Availability
- Available in iPhone OS 2.0 and later.
Declared In UIApplication.h applicationDidFinishLaunching:
Tells the delegate when the application has finished launching. This method is optional.
- (void)applicationDidFinishLaunching:(UIApplication *)application Parameters
application
The delegating application object.
Discussion
This method is the ideal place for the delegate to perform various initialization and configuration tasks, especially restoring the application to the previous state and setting up the initial windows and views of the application.
The application posts a UIApplicationDidFinishLaunchingNotification at the same time it calls this method. Availability
- Available in iPhone OS 2.0 and later.
See Also
- – applicationWillTerminate:
Declared In UIApplication.h applicationDidReceiveMemoryWarning:
Tells the delegate when the application receives a memory warning from the system. This method is optional.
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application Parameters
application
The delegating application object.
Discussion
Your implementation of this method should free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. You use this method in conjunction with the didReceiveMemoryWarning of the UIViewController class and the UIApplicationDidReceiveMemoryWarningNotification notification to release memory throughout your application.
It is strongly recommended that you implement this method. If your application does not release enough memory during low-memory conditions, the system may terminate it outright. Availability
- Available in iPhone OS 2.0 and later.
See Also
- – didReceiveMemoryWarning (UIViewController)
- UIApplicationDidReceiveMemoryWarningNotification
Declared In UIApplication.h applicationSignificantTimeChange:
Tells the delegate when there is a significant change in the time. This method is optional.
- (void)applicationSignificantTimeChange:(UIApplication *)application Parameters
application
The delegating application object.
Discussion
Examples of significant time changes include the arrival of midnight, an update of the time by a carrier, and the change to daylight savings time. The delegate can implement this method to adjust any object of the application that displays time or is sensitive to time changes.
The application posts a UIApplicationSignificantTimeChangeNotification at the same time it calls this method. Availability
- Available in iPhone OS 2.0 and later.
Declared In UIApplication.h applicationWillResignActive:
Tells the delegate that the application will become inactive. This method is optional.
- (void)applicationWillResignActive:(UIApplication *)application Parameters
application
The singleton application instance.
Discussion
The delegate can implement this method to make adjustments when the application transitions from an active state to an inactive state. When an application is inactive, it is executing but is not dispatching incoming events. This occurs when an overlay window pops up or when the device is locked.
Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification. Availability
- Available in iPhone OS 2.0 and later.
Declared In UIApplication.h applicationWillTerminate:
Tells the delegate when the application is about to terminate. This method is optional.
- (void)applicationWillTerminate:(UIApplication *)application Parameters
application
The delegating application object.
Discussion
This method is the ideal place for the delegate to perform clean-up tasks, such as freeing allocated memory, invalidating timers, and storing application state.
The application posts a UIApplicationWillTerminateNotification at the same time it calls this method. Availability
- Available in iPhone OS 2.0 and later.