Game Development Reference
In-Depth Information
Listing 2-1. main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
The main function in Listing 2-1 is a C function, but you will notice that the body of the function
is clearly Objective C syntax. An @autoreleasepool wraps a call to UIApplicationMain . The
@autoreleasepool sets up the memory management context for this application; we don't really need
to worry about that. The function UIApplicationMain does a lot of handy stuff for you: it initializes
your application by looking at your info.plist file, sets up an event loop, and generally gets things
going in the right way. In fact, I have never had any reason to modify this function because iOS
provides a concise place to start adding your startup code. The best place to start adding initialization
code is the task application:didFinishLaunchingWithOptions: found in the implementation file of
your app delegate class. In this example, the app delegate class is called AppDelegate . Listing 2-2
shows the implementation of the application:didFinishLaunchingWithOptions: task from
AppDelegate.m .
Listing 2-2. application:didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController_iPhone alloc]
initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController_iPad alloc]
initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
When an application is fully initialized and ready to start running the code that comprises it, the task
application:didFinishLaunchingWithOptions: is called, as seen in Listing 2-2. This task, besides
having an incredibly long name, takes an instance of UIApplication and NSDictionary as arguments.
The UIApplication is an object that represents the state of the running application. The instance of
AppDelegate that receives this message is the delegate to the UIApplication object. This means
that we modify the application's behavior by implementing tasks in AppDelegate . The protocol
 
Search WWH ::




Custom Search