Home / iPad


UIApplication loads the parts of the MainWindow.xib file as follows:

  1. Creates DeepThoughtsAppDelegate.
  2. Creates Window.
  3. Sends the DeepThoughtsAppDelegate the application:didFinish LaunchingWithOptions: message.
  4. DeepThoughtsAppDelegate initializes the window.

The header and implementation of DeepThoughtsAppDelegate in Listings-1 and Listings-2. All this is done for you as part of the Xcode template.

Listing-1: DeepThoughtsAppDelegate.h
#import <UIKit/UIKit.h>
@class DeepThoughtsViewController;

@interface DeepThoughtsAppDelegate : NSObject
	  <UIApplicationDelegate> {
    UIWindow *window;
    DeepThoughtsViewController *viewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet
	  DeepThoughtsViewController *viewController;
Listing2: DeepThoughtsAppDelegate.m
#import "DeepThoughtsAppDelegate.h"
#import "DeepThoughtsViewController.h"
@implementation DeepThoughtsAppDelegate

@synthesize window;
@synthesize viewController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didF
	inishLaunchingWithOptions:(NSDictionary *)
	launchOptions {

    // Override point for customization after app launch
    // Add the view controller's view to window and
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

	return YES;
- (void)applicationWillResignActive:(UIApplication *)
	application {
     Sent when the application is about to move from
	active to inactive state. This can occur for
	certain types of temporary interruptions (such
	as an incoming call) or when the user quits the
	application and it begins the transition to the
	background state.
     Use this method to pause ongoing tasks, disable
	timers, and throttle down OpenGL ES frame
	rates. Games should use this method to pause
	the game.
- (void)applicationDidEnterBackground:(UIApplication *)
	application {
     Use this method to release shared resources, save
	user data, invalidate timers, and store enough
	application state information to restore your
	application to its current state in case it is
	terminated later.
     If your application supports background execution,
	called instead of applicationWillTerminate:
	when the user quits.
- (void)applicationWillEnterForeground:(UIApplication *)
	application {
     Called as part of transition from the background to
	the inactive state: here you can undo many of
	the changes made on entering the background.
- (void)applicationDidBecomeActive:(UIApplication *)
	application {
     Restart any tasks that were paused (or not yet
	started) while the application was inactive. If
	the application was previously in the
	background, optionally refresh the user interface.
- (void)applicationWillTerminate:(UIApplication *)
	application {
    Called when the application is about to terminate.
    See also applicationDidEnterBackground:.

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication
	*)application {
     Free up as much memory as possible by purging cached
	data objects that can be recreated (or reloaded
	from disk) later.
- (void)dealloc {
     [viewController release];
     [window release];
     [super dealloc];

In Listing-1-2, the view controller is initialized with the applicationDid FinishLaunchingWithOptions method, which you can use to do any other application initialization as well, such as returning everything to what it was like when the user last used the application.

Remember Your goal during startup should be to present your application's user interface as quickly as possible - quick initialization = happy users. Don't load large data structures that your application won't use right away. If your application requires time to load data from the network (or perform other tasks that take noticeable time), get your interface up and running first and then launch the slow task on a background thread. Then you can display a progress indicator or other feedback to the user to indicate that your application is loading the necessary data or doing something important.

When the application:didFinishLaunchingWithOptions: method is invoked, your application is in the inactive state. Unless your application does some kind of background processing, when your application becomes active, it will receive the applicationDidBecomeActive: message when it enters the foreground (becomes the application the user sees on the screen), which explain in "Responding to interruptions" in this tutorial.

With iOS 4.2, an application can also be launched into the background, but because the DeepThoughts application at the heart of this section doesn't do any background processing. And because the application does no background processing, there's also nothing it has to do in response to the applicationDidBecomeActive: message.

The application delegate object (refer to Listing-1) is usually derived from NSObject, the root class (the very base class from which all iPad application objects are derived), although it can be an instance of any class you like, as long as it adopts the UIApplicationDelegate protocol. The methods of this protocol correspond to behaviors that are needed during the application lifecycle and are your way of implementing this custom behavior. Although you aren't required to implement all the methods of the UIApplicationDelegate protocol, every application should implement the following critical application tasks:

  • Initialization
  • Handling events
  • Responding to interruptions
  • Responding to termination
  • Responding to low memory warnings
[Previous] [Contents] [Next]