Chapter 4. Anatomy of an iPhone Application

This chapter discusses the basic steps needed to build a simple iPhone application. Section 4.1 demonstrates the basic structure of a simple iPhone application. Next, Section 4.2 shows the steps needed to write the application using XCode. Finally, Section 4.3 summarizes the chapter.

Hello World Application

This section demonstrates the basic structure of a simple iPhone application that simply displays the message Hello World to the user. Follow these steps to develop the application.

Create a main.m file

As in any C program, the execution of Objective-C applications starts from main(). You need to create the main() function in the main.m file as follows:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc,argv,nil,@"HelloWorldAppDelegate");
  [pool release];
  return retVal;
}

The main() function starts by creating an autorelease pool and ends by releasing it. In between, it makes a call to the UIApplicationMain() function. UIApplicationMain() is declared as follows:

int UIApplicationMain(int argc, char *argv[],NSString *principalClassName,
                      NSString *delegateClassName)

This function takes four parameters. The first two parameters are the arguments passed in to the main() function. These parameters should be familiar to any C programmer. The third parameter is the name of the application class. If a nil is specified, the UIApplication class is ...

Get iPhone SDK 3 Programming: Advanced Mobile Development for Apple iPhone and iPod touch now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.