Tuesday 21 February 2012

First iPhone app

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!This is how your comleted application looks.












 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.

Thank you!!
 

No comments:

Post a Comment