Delphi to Cocoa

July 9, 2009

Bootstrapping an iPhone Application

Filed under: iPhone — rithban @ 10:24 pm
Tags: , , , ,

The Way It Was

To bootstrap a GUI program in Delphi, you use the global Application object instance. It provides the core event-driven framework for a Microsoft Windows application. If you look at the source code of a new application as found in the .dpr file, you’ll see code like:

  Application.CreateForm(TForm1, Form1);

Delphi did a lot of work for the programmer in automatically creating a TForm subclass including the visual object resources (in the .dfm file) as well as the bootstrapping code.

If you wanted to be hard core, you could discard the default form subclass, instantiate a raw TForm and attach it to the Application global instance. But two of the three lines in the code wouldn’t change. You must still call


Do something to create a main form.



Raw Bootstrapping an iPhone Application

iPhone applications require a little more setup, but XCode pretty much does the same thing for the developer. It creates a default window and it Interface Builder resource file similar to how Delphi would create a default TForm subclass and its .dfm file.

As with everything, bootstrapping an iPhone application from scratch is a little more involved than how it use to be with Delphi… but it’s not really difficult.


As with all C-derived languages, main() is the program entry point called by the operating system.

Here is what the default main.m source file looks like.

#import <UIKit/UIKit.h>

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

As you can see, main() sets up the autorelease pool then disappears into UIApplicationMain() and never returns until the application terminates. In fact Apple’s documentation states that either this function returns 0 (success), or the program dies unceremoniously. That seems rather extreme to never return with an error, but that’s how the software is designed.

The Application Delegate

The application delegate is a class that handles setup and tear down of the application. It’s loosely related to Delphi’s TApplication.

When bootstrapping an application, UIApplicationMain() will look in the application’s plist (configuration file) so it knows which which application delegate to hand control over to. If you’re manually bootstrapping the application, that information will not be in the application plist. You’ll need to pass the name of the application delegate class as the fourth parameter to UIApplicationMain(), for example.

int retVal = UIApplicationMain(argc, argv, nil, @"Project1AppDelegate");

UIApplicationMain() will instantiate the application delegate class, then invoke its applicationDidFinishLaunching() method. This method will initialize the GUI, similar to what

  Application.CreateForm(TForm1, Form1);

did, but things are in different places. If interface builder is used to create the window, the application delegate will have an IBOutlet property for the window.

This article is bootstrapping a do-nothing application from scratch, so the application delegate will also contain the view instance. For example,

#import <UIKit/UIKit.h>

@interface Project1AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    UIView *view;

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


The applicationDidFinishLaunching() method implementation may look like:

#import "Project1AppDelegate.h"

@implementation Project1AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Get size of application frame
    CGRect lFrame = [[UIScreen mainScreen] applicationFrame];
    // Create main window
    self.window = [[UIWindow alloc] initWithFrame: lFrame]; 
    // Create main view for window
    lFrame.origin.y = 0.0;
    view = [[UIView alloc] initWithFrame: lFrame];
    // Attach view to window
    [window addSubview: view];

    // TForm.Show
    [window makeKeyAndVisible];

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


At this point, the application has been successfully bootstrapped, and the main event handler loop is running. Thus, this is similar to the Delphi Application.Run.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: