![]()
Today I was asked a good question for which I didn't have a good answer. Andy, one of my wife's co-workers was interested in starting iPhone application development and wanted to know if there were any good books on the subject. I didn't recall which books I had used to learn but I said the language and a somewhat steep learning curve, and that I started out with a relatively simple book, but then quickly moved on to other sources once I'd gotten the hang of things.
First things first. The book I did start with was James Brannon's iPhone SDK Programming, A Beginner's Guide. It was a decent starting book for me, but I dropped it half way through in favour of Apple's own iPhone OS Reference Library website (which ironically, doesn't work from Safari on my iPod Touch).
One of the best ways of gauging the complexities of a language is to look at what's involved in creating a "Hello World" application, a simple program that prints the text "Hello World". Programming languages like BASIC can do this in a single line. Languages like C take a few lines, and other languages like Java take even more. Even HTML and PHP require a certain amount of infrastructure to get the most basic program going.
iPhone apps are written using a program called XCode, which is free to download, but only runs on Mac OS X. Apps can be created and run in an iPhone Simulator and you don't actually need an iPhone or an iPod touch to create a working iPhone app, but you do need a Mac. I ended up purchasing a Mac Mini exclusively to program iPhone apps.
There's a lot of extra detail that I'm not going to go into here, including things like singing up for a Developer license so that you can move these applications onto an iPhone. Any good iPhone book will take about a chapter describing that process. It's a relatively painful one that thankfully only needs to be done once. I'm also assuming that you'll be able to find XCode from Apple's iPhone Dev Center.
![]()
To create a new iPhone project, start XCode, and select File > New Project.
Create a "View-based Application" with the name HelloWorld.
You'll notice that XCode creates over a dozen files just for a simple project! There's actually a fair division of labour here.
First, a little primer about Objective-C/Cocoa. Each class in Objective C is defined in two files, one ending in ".h" and another ending in ".m". Files ending in ".h" are "Header" files, and define the methods (or functions) in each class without actually doing anything. ".m" or method files contain all those functions. Objective-C separates these out so that the headers of a class can be loaded up in another file without all the functions being loaded up too.
Of these 13 files and frameworks, 5 of them are relevant for making the first iPhone app:
HelloWorldAppDelegate.h and HelloWorldAppDelegate.m: These files are the "glue that holds the application together. An application is usually composed of several Views, each with their own View Controller. The App Delegate handles transition and communication between these different view controllers. Since we're only creating a simple app with a single view, the default AppDelegate is just fine and doesn't need to be changed.
HelloWorldViewController.h and HelloWorldViewController.m. These files provide the "meat" of the program and contain the code that actually does anything. Our View Controller will define a Label, and a method that will change that Label to "Hello World".
HelloWorldViewController.xib is a "nib" file that defines how the interface looks.
Let's start by building the interface. Double Click on HelloWorldViewController.xib
In the "View" window, click on the left-most tab "Attributes"
Change the background color to something other than grey. I've chosen orange.
In the Library window, drag and drop a "Label" and "Rounded Rect Button" into the view. Change the size of the Label by dragging on its edges, and center it using the "Layout" function. Double click on the button to change its name to "Button". You can click on the third tab of the "View" window and center each object with the placement buttons.
Save and quit the interface builder. Right now we have an interface that doesn't actually do anything, but we'll fix that soon.
Now we're back in XCode. Click on the "HelloWorldViewControler.h" file, and add these lines in italics:
#import
@interface HelloWorldViewController : UIViewController {
IBOutlet UILabel *myLabel;
}@property (nonatomic,retain) UILabel *myLabel;
- (IBAction) touchButton: (id) sender;
@end
Now click on the "HelloWorldViewController.m" file and add the following after the @import line:
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
@synthesize myLabel;- (IBAction) touchButton: (id) sender {
[myLabel setText:@"Hello World"];
}
and for good measure at the bottom, make sure to release myLabel once we're done with it. Variables that were retained (in our .h file) need to be released with the program is finished running.
- (void) dealloc {
[myLabel release];
[super dealloc];
}
There's a lot of methods commented out below this that handle
The "[myLabel setText:@"Hello World"];" is an interesting one, and would stand out as strange to anyone having programmed only in C or C++ before. Objective-C defines "messages" in square brackets, and "[myLabel setText:@"Hello World"];" means "Take the object myLabel, and call it's "setText" method with the string @"Hello World".
Select Build > Build (or Command-B) and build the project. This is an important step. Without this, the Interface builder won't be able to see the label (myLabel) and method (touchButton:) we created.
Double click on HelloWorldViewController.xib and bring up the Interface Builder again.
In the "HelloWorldController.xib" window, click on "File's Owner"
Click on the second tab of the Inspector to bring up "Hello World View Controller Connections"
Drag and drop from the small circle to the right of "myLabel" to the Label in the View window.
Drag and drop from the small circle to the right of "touchButton:" to the Button, then select "Touch Up Inside" from the menu.
Save and quit the interface builder. Everything should now be connected up and ready to go!
In Xcode, select Build > Build and Run (Command-Enter) to run the app in the iPhone Simulator.
Click the button and see "Label" change to "Hello World".
A fair amount of work to get even a simple program going. Thankfully, once you get past this relatively steep cliff, creating more complicated programs is much easier. (Memory management can still be a little tricky, but not too bad once you get the hang of it.) I went from simple programs like this, to programs with multiple buttons that performed different actions, and eventually on to programs with multiple views, graphics and sound.
I hope this was helpful, at least in giving people a sense that while iPhone app development starts with a steep cliff, there's a nice plateau of fun development afterward.
