{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
Application Delegate for the First iPhone Application Explained
Summary
This article explains the Application Delegate of an iPhone application. This article discusses the standard application delegate header and implementation files generated for a navigation based application. Also goes through the methods that are generally implemented from the UIApplicationDelegate protocol.
 
Table of Contents

Creation of the first iPhone application

The source files

The Header File

Code Listing 1. The Application Delegate Declaration

What is an application delegate?

Figure 1. The variable window of an iPhone application. Here, application object is of type UIApplication; and it has a delegate associated with it (called FirstAppAppDelegate).

The Signature of Application Delegate

Code Listing 2. The signature of application delegate.

The Implementation File

Code Listing 3. Implementation file for the application delegate

The Methods

applicationDidFinishLaunching

Figure 2. The call stack leading to applicationDidFinishLaunching. The code in this method gets executed at the beginning of the application.

Code Listing 4. The UIApplicationDelegate protocol

Code Listing 5. The delegate declaration in UIApplication

Code Listing 6. The applicationDidFinishLaunching method

applicationWillTerminate

Code Listing 7. The applicationWillTerminate method

Figure 3. The callstack leading up to the termination of application

dealloc

Code Listing 8. The dealloc method

 
Article Series
Previous Article:
Test Your Knowledge: Understanding a Typical main Method in Objective-C for iPhone Development
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: Application Delegate for the First iPhone Application Explained

Creation of the first iPhone application

The first iPhone application discussed in this article has been created in the following article:

Creation of First iPhone application explained

This project is a simple navigation-based application. It uses a table view on the main window. From the code side, a table view controller (UITableViewController) is used to provide the functionality for a table based view.

The following article discusses the source code of the application delegate.

The source files

The application delegate is in two files:

  • FirstAppAppDelegate.h
  • FirstAppAppDelegate.m

The Header File

The header file declares application delegate.

Code Listing 1. The Application Delegate Declaration

//
// FirstAppAppDelegate.h
// FirstApp
//

#import <UIKit/UIKit.h>

@interface FirstAppAppDelegate : NSObject <UIApplicationDelegate> {
    
    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

What is an application delegate?

Every iPhone application (for the normal scenarios) will have one and only one (singleton) object of type UIApplication. This provides some basic functionality for running and managing an application. This class is from the Cocoa Touch library. It is defined in UIApplication.h, which is part of the UIKit framework.

Now, there needs to be a way to add your own custom code to respond to some of the events that UIApplication is responding to. That’s where the delegate comes into play. This is typically setup as a connection from the Interface Builder.

Figure 1. The variable window of an iPhone application. Here, application object is of type UIApplication; and it has a delegate associated with it (called FirstAppAppDelegate).

Figure 1. The variable window of an iPhone application. Here, application object is of type UIApplication; and it has a delegate associated with it (called FirstAppAppDelegate).

As you can see from the figure above, the application object has a delegate, which will respond to events like applicationDidFinishLaunching and applicationWillTerminate. These methods include a parameter like (UIApplication *)application. This is the singleton object shown above.

The Signature of Application Delegate

The Objective-C keyword @interface says this is the declaration of a class in this program.

Code Listing 2. The signature of application delegate.

@interface FirstAppAppDelegate : NSObject <UIApplicationDelegate>

FirstAppAppDelegate is the name of the Application Delegate for this iPhone application. If we separate the name (FirstApp + AppDelegate), FirstApp is the name of the iPhone application.

NSObject (which follows a colon :) is the base class from which the FirstAppAppDelegate is derived from. NSObject is the standard base class for all the classes in Cocoa and CocoaTouch frameworks. It provides some very basic functionality that every object should support.

The last word in the signature of the application delegate class is UIApplicationDelegate, which is a protocol (like an interface in C# and Java). Because of the inclusion of this protocol, you might be implementing methods like applicationDidFinishLaunching and applicationWillTerminate.

The Implementation File

Following is the implementation file of the application delegate:

Code Listing 3. Implementation file for the application delegate

//
// FirstAppAppDelegate.m
// FirstApp
//

#import "FirstAppAppDelegate.h"
#import "RootViewController.h"


@implementation FirstAppAppDelegate

@synthesize window;
@synthesize navigationController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}


- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}

@end

The Methods

Let’s discuss the methods.

applicationDidFinishLaunching

An Objective-C program starts in the main() method, like any C application. Outside of main() method, applicationDidFinishLaunching method is the first method from your UI code that gets invoked.

Figure 2. The call stack leading to applicationDidFinishLaunching. The code in this method gets executed at the beginning of the application.

Figure 2. The call stack leading to applicationDidFinishLaunching. The code in this method gets executed at the beginning of the application.

As you can see from the callstack, the UIApplicationMain in the main() method is responsible for calling (eventually) this overridden method (applicationDidFinishLaunching). This method is in the class FirstAppAppDelegate class, which implements the UIApplicationDelegate protocol.

The applicationDidFinishLaunching is an optional method from the UIApplicationDelegate protocol. The application delegate that you write for your iPhone application will implement some of the methods declared in this protocol.

Code Listing 4. The UIApplicationDelegate protocol

@protocol UIApplicationDelegate<NSObject>

@optional

- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url; // no equiv. notification. return NO if the application can’t open for some reason

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application; // try to clean up as much memory as possible. next step is to terminate app
- (void)applicationWillTerminate:(UIApplication *)application;
- (void)applicationSignificantTimeChange:(UIApplication *)application; // midnight, carrier time update, daylight savings time change

- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame; // in screen coordinates
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;

@end

As you can see from the declaration of the protocol, a whole bunch of application behavior related methods are declared here. You can also see all of them provide an object of type UIApplication as parameter.

Code Listing 5. The delegate declaration in UIApplication

UIKIT_EXTERN_CLASS @interface UIApplication : UIResponder
{
  @package
    id <UIApplicationDelegate> _delegate;

...

@property(nonatomic,assign) id<UIApplicationDelegate> delegate;

The UIApplication object knows which class is the delegate in the code (this connection was made from the Interface Builder). So, once the UIApplication object is done with all the initialization aspects of the application, it will call the applicationDidFinishLaunching method.

Code Listing 6. The applicationDidFinishLaunching method

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}

As you can see from the code, you want to set up and show the main window here. The addSubview method is used to add a view to the window. And then you would make this a key window (i.e. gets the keyboard events) and make it visible by using the method makeKeyAndVisible.

applicationWillTerminate

The applicationWillTerminate method will get called when the application is about to be closed. So, this would be the place to save any data that belongs to the application, if necessary.

Code Listing 7. The applicationWillTerminate method

- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}

Figure 3. The callstack leading up to the termination of application

Figure 3. The callstack leading up to the termination of application

dealloc

And finally, the dealloc method deallocates the memory for the objects in this application. The dealloc method is part of NSObject class, which is the root class of Cocoa and Cocoa Touch objects.

Code Listing 8. The dealloc method

- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}

Here release message is sent to navigationController and window objects. These two are instance variables in this application delegate class. This code might not be hit, because, in this particular case, the application will be closing before the dealloc is called.

Take a Quick Quiz on this Article

1. Which protocol would the custom application delegate class implement?



: It’s a delegate for the UIApplication.
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: Understanding a Typical main Method in Objective-C for iPhone Development
This article is part of the Series:
Article Series: Getting started with iPhone development
Next Article:
Test Your Knowledge: Application Delegate for the First iPhone Application Explained
Bookmark and Share This

More Articles With Similar Tags
This is a test on the article that gives a quick overview of the development tools installed with iPhone SDK. These tools include Xcode, Interface Builder, Instruments, and iPhone Simulator. In the article, Dash Code, which is used to build web applications for iPhone is also introduced. GDB, the open source debugger, is discussed there as well.
icon-xcode-groups-files.jpg
This article discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
This is a test on the article that discusses various parts of the Project window inside an Xcode project. It also gives a quick overview of the Groups and Files navigation window. The Run menu, the default toolbar, and the built-in source editor are also discussed.
icon-xcode-interface-builder-connection-panel.jpg
This article explains the user interface files of an iPhone application. Explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
This is a test on the article that explains the user interface files of an iPhone application. It explains the contents of the .xib files, which store the user interface. Goes through the various windows (Document Window, User Interface Window, Inspector Window, Library Window, Connections Panel) of the Interface Builder. The example used is a simple navigation-based iPhone application.
About  Contact  Privacy Policy  Site Map