Skip to Content

iPhone Dev 101: The "Hello World!" app

In the last iPhone Dev 101 post, I told you a little about creating your first project using Xcode; however, in this post, I want to show you how to create your first application that will run in the iPhone simulator. In honor of staying with the classic way of teaching programming, we'll create a "Hello World!" application as our first one.

Creating the new project
If you have installed the iPhone SDK/Xcode, then you can launch Xcode by navigating to /Developer/Applications. Once there, you can double click on the Xcode application (you may also find it handy to just drag the icon to the dock if you will be using it a lot).

Once Xcode launches, click File > New Project. Under the iPhone OS section on the left side of the resulting window, select "Application." Select "View-based Application" from the templates that show up on the right side, and then click the "Choose" button. You will then be prompted to specify a project save name -- this will also be the name of your resulting application, so choose your project name wisely.

You're project has now been created, and the Xcode window that is displayed will contain all of your code, resources, etc. There isn't much there now, but the application is fully functional at this point. You can click the "Build & Go" button in the toolbar, and the application will be compiled and launched in the iPhone Simulator. Again, this is a fully functional application, but it doesn't do anything useful at this point -- the usefulness of the app is up to your coding, but Apple supplies you with the base code and dependencies.Using .xib files
XIB files are a "newer" format of Apple's older NIB files. These files are where the user interface behind the Mac & iPhone applications is saved/packaged. The history of the NIB file is quite interesting and stretches back to the days of NeXT (you can read more about the history of Interface Builder here). You edit the user interface in an application called Interface Builder, which saves to these file types. When you double click on one of the files in Xcode, Interface Builder (IB) will automatically launch. Let's double click on the MainWindow.xib file.

Once it loads, IB will display a mockup of an iPhone OS view. This view can have objects (buttons, text, etc.) placed on it, and linked to in the code. For this project, however, let's make sure the Library window is open: Click Tools > Library (or press shift + command + L). In the Library, you will find all of the components that can make up an iPhone application. In the Library window, select "Objects," and then click Library; In the search box type "text." The search results will return two objects, but the one we want is the "Text View," so drag that to the "Hello World View Controller" window, and the object will be "created." If you double click where you just dragged the text view, then you will be able to edit the text that will be shown when the app launches. You can type what ever you wish, but we'll use "Hello World!"

Once you have your text typed, select File > Save, from within IB; the information will be saved in the .xib file. Now close IB -- you should see the Xcode application still opened.

Running your "Hello World!" iPhone app
Select "Build and Go" from the toolbar in Xcode. It will save the current project, compile the source code (along with the .xib file), and run it inside of the iPhone simulator. If you get an error while compiling, make sure that the build settings are set to "Simulator - iPhone OS 2.2.1." You change this by clicking on the Overview drop-down menu on the tool bar; You can also change this by going to the Project > Set Active SDK menu.



Your application should launch in the simulator and display the text "Hello World!" So, congrats on your first iPhone application! Now you're ready to start writing much cooler apps that take full advantage of the iPhone. If you don't have Objective-C (or Cocoa) knowledge, I recommend that you backtrack to the post about useful Cocoa resources.

In the next iPhone Dev 101 post, I'll talk about taking testing one step further by creating an Ad-hoc copy of your applications.

In the last iPhone Dev 101 post, I told you a little about creating your first project using Xcode; however, in this post, I want to show...
 

Add a Comment

*0 / 3000 Character Maximum

13 Comments

Filter by:
Amerist

And this is relevant because...

June 08 2009 at 3:59 AM Report abuse rate up rate down Reply
Frank

Folks, for those interested in more iPhone programming info, in case you missed it, TUAW has an incredibly valuable piece of information here:

http://www.tuaw.com/2009/04/06/iphone-developers-get-ivy-league-edu-mu-cation/

That's right, THE Stanford University is offering their iPhone course online... FREE. And though I've only glanced at it, it looks like it may fit the bill, especially for those of you who are quite new to the Mac programming environment and prefer a visual/classroom like environment. Did I mention it was FREE?

I strongly recommend that those who would like to learn but find the process challenging to point their iTunes at this video podcast and let it download. At least give it a shot. It costs nothing but the bandwidth to pull down the video (and yes, they are quite large... but probably no larger than buying a HD episode of The Office).

And there are tons of online resources from websites/blogs/etc. with tutorials, etc., to audio/video podcasts.

And, of course, there are books you can invest in if you're more the reader. If so, I'd recommend

Cocoa(R) Programming for Mac(R) OS X (3rd Edition) by Aaron Hillegass
[ http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=sr_1_1?ie=UTF8&s=books&qid=1241023461&sr=1-1 ]

and for a solid understanding of Objective-C itself,

Programming in Objective-C 2.0 (2nd Edition) by Stephen Kochan
[ http://www.amazon.com/Programming-Objective-C-2-0-Developers-Library/dp/0321566157/ref=sr_1_1?ie=UTF8&s=books&qid=1241023442&sr=1-1 ]

[NOTE: Amazon links only to show cover, not meant as endorsement of where to buy.]

Anyway, best of luck to those interested. It can be quite rewarding, not to mention fun, to write your own programs.

April 29 2009 at 12:50 PM Report abuse rate up rate down Reply
wplate

Assuming we have the capability, how does this application get transferred to a device to run there instead of in simulation?

April 28 2009 at 10:46 AM Report abuse rate up rate down Reply
1 reply to wplate's comment
Erick

Follow apple's pretty detailed how to on their Portal

April 28 2009 at 3:38 PM Report abuse rate up rate down Reply
Erick

Im a VB baby with ZERO college education so when I picked up the SDK and started kicking it around...I wont lie. I think I peed a little.

After much trial and error and ALOT of hacking, I eventually managed to make 3 apps that are now selling in the store.

It's possible and once you understand the very basics of it all, anyone with some programming history should be able to accomplish it.

There is so many resources out now that any idiot (me included) can make an app and make a few bucks from it. I just encourage you make useful apps that people want. If you make an app, repeat it 50 times with a different title and expect millions, forget it.

April 28 2009 at 10:14 AM Report abuse rate up rate down Reply
Jefflyall94

more please :-) thanks

April 28 2009 at 9:14 AM Report abuse rate up rate down Reply
Jim

More programming! More! Faster! Better!

Seriously, there could be more room in TUAW for a programming section, especially considering how hot iPhone coding is.

Waiting for the next one

April 28 2009 at 2:02 AM Report abuse rate up rate down Reply
Cory

Great post!

I would love to start coding an iPhone app, or start coding anything in general, but it seems so confusing and daunting that I know I will never give it a chance unless I physically meet someone and they help me through it.

Oh well, one can dream about it I suppose...

April 27 2009 at 11:17 PM Report abuse rate up rate down Reply
Max

The fee is to keep riff raff out of the community. I think they have it for the very reason so that people don't just walk around and go "Hey, Look at what I coded," because you can put 100 iphones on that account. Also, they don't want everyone getting 3.0

April 27 2009 at 9:31 PM Report abuse rate up rate down Reply
J.T. Mill

Is it possible to upload a program to your phone without paying that annoying $100 fee? I just wanna be able to walk around and go, "Hey! Look at what I coded!" ^^;

April 27 2009 at 9:17 PM Report abuse rate up rate down Reply
Marco

Even that looks hard ;_;

April 27 2009 at 4:42 PM Report abuse rate up rate down Reply
2 replies to Marco's comment
Lee (UK)

Tell me about it... I've managed to get an app on my iPhone but i can't get the profile right to upload it to the store... Grrrrrr

I'd buy some help if i could at this stage just to finish the thing.

April 27 2009 at 5:13 PM Report abuse rate up rate down Reply
Brian Reading

Well, if that sounds hard, you probably won't get very far in iPhone or Mac OS X application development using Cocoa Touch and Cocoa.

I recommend that you get some more basics under your belt before you dive into that stuff. I learned some C and did some .NET, and now this stuff makes a lot more sense.

April 27 2009 at 6:15 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.