Simple iPhone “Hello World” tutorial.
March 10th, 2008UPDATE: This tutorial no longer works with the latest iPhone sdk. I recommend this tutorial instead:
http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/
Here is a simple Hello World iPhone app that does nothing else but display “hello world”.
1. In Xcode create a new “Cocoa Touch Application” Project called “HelloWorld”
2. Open HelloWorldAppDelegate.m in the Classes folder
3. Add this code to the bottom of the applicationDidFinishLaunching method:
1 2 3 4 | UITextField *textView = [[UITextView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; [contentView addSubview:textView]; [textView setText:@"Hello World"]; [textView release]; |
The entire method should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | - (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create window
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Set up content view
self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
[window addSubview:contentView];
// Show window
[window makeKeyAndVisible];
UITextField *textView = [[UITextView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
[contentView addSubview:textView];
[textView setText:@"Hello World"];
[textView release];
} |
4. Click “Build and Go”.
5. Hello World
August 4th, 2008 at 10:15 pm
I realize this may be pedantic, but this isn’t really the end of the story. You now have to run the code on an actual iphone!
January 5th, 2009 at 4:01 am
Josh, not pedantic at all – The above is just the start. There’s a lot you need to do to run it on an actual phone!
January 5th, 2009 at 10:28 am
Thanks Ade. This is just a start and your right josh, there is more work to do to get this thing running on an iPhone. Although that requires a tutorial on its own. Luckily Apple gives good info on their dev site but its only accessible once you have been accepted to the dev program. When I wrote this tutorial I hadn’t yet been accepted into the iPhone dev program so was unable to test on the phone.