1.4. Building and Installing Applications

Now that you've installed the iPhone SDK, the next step is to learn how to use it. The iPhone SDK functions as a component of the Xcode IDE. To launch Xcode, navigate to the newly created /Developer directory on your hard drive. Open a finder window and then click the disk icon in the sidebar. Now double-click the Developer folder, followed by Applications. Drag the Xcode application onto your dock to make launching it easier.



The /Developer/Applications folder is separate from Mac OS's default Applications folder. You can drag the folder onto the dock as a stack to have all developer applications easily accessible, or drag it onto the sidebar to click through to it using the Finder. You'll be accessing this folder a lot as you continue to develop software, so you'll want to make it easy to get to.

After launching Xcode, select New Project from the File menu. A window will appear, prompting you to select a template for your new project. Underneath the iPhone OS heading, click the Application category. You'll see several different types of application templates to choose from, as shown in Figure 1-3.

Figure 1-3. Available iPhone application templates

1.4.1. Model-View-Controller

Software development on the iPhone follows the model-view-controller (MVC) paradigm. The goal of this architecture is to abstract business logic, such as your application's data and the rules that govern it, from the user interface (UI) components displayed to the end user. Three key pieces are needed to implement MVC. The model represents the data and business logic for your application. The view represents the UI elements that present the data to the user and allow them to act on it. The controller provides the interaction between the UI elements and the data, such as responding to multi-touch gestures, interaction events, and transitioning between different portions of the logic.

You'll see these concepts reflected in the names given to many iPhone classes. In many cases, controllers will also encapsulate a view, making it easy to control the view without having to write very much code to connect them.

1.4.2. Application Templates

Xcode provides several skeletons for implementing the MVC architecture in your application. The following templates are most commonly used:

View-based application

Applications using only one view should use this template. A simple view controller manages the application's primary view, using an interface-builder template for layout (although we'll show you how to remove this and build your own if you like). Simple applications without any navigation should use this template. If your application requires navigation through multiple views, consider using the navigation-based template.

Navigation-based application

The navigation-based template is ideal for applications that traverse multiple views and require a means of navigation between them. If you can envision your application having screens with "Back" buttons in them, chances are you should use this template. A navigation controller handles all of the internal work in setting up navigation buttons and transitioning between "stacks" of views. This template provides a basic navigation controller and a root (base-tier) view controller to display information.

Utility application

Ideal for widget-type applications, where an application has a main view that you can "flip" around like a widget in Leopard. You may also be familiar with these from Konfabulator (the third-party predecessor to Apple's Dashboard). The iPhone's Weather and Stocks applications are good examples of utility applications. This template also includes an information button that causes the view to flip around to display the application's flipside, which is commonly used to change settings or the information displayed.

OpenGL ES application

If you're creating 3D games or graphics, use this template. It creates a view configured to render a GL scene and provides a sample timer to animate it. OpenGL programming is not covered in this book.

Tab bar application

Provides a special controller displaying a button bar along the bottom of the screen. This template is ideal for applications such as the iPod or Mobile Phone applications, where a row of tabs along the bottom provides a series of shortcuts to the core functionality of the application.

Window-based application

If none of the other five templates suits your needs, this very basic template provides a simple application with a window. This is the bare minimum framework you'll need in order to start your own application.

1.4.3. Xcode Project Layout

After creating a new project, its contents will be laid out in a self-contained window like Figure 1-4. The project encapsulates the sources, frameworks, and resources for the application.

Figure 1-4. A newly created iPhone project

The following groups can help organize your project:


The files containing the Objective-C classes that your applications use. These include the application delegate object, view controllers, and other objects you create. As you add new class files to your application, they will appear in this group.

Other sources

Other sources compiled into your application. By default, this includes precompiled headers and your application's main function, which instantiates the Cocoa application object. If your application incorporates additional C functions or C++ classes, add the files here.


Application resources that are not compiled in with your application's source code, but are copied into the program folder when the application is built. These can include images or sounds, game levels, or other important files.


The frameworks that your application links with. These are shared libraries linked at build time to add functionality. For example, a 3D game will link with the OpenGLES framework, which contains the routines to render 3G graphics. Sound-intensive applications will likely take advantage of the Audio Toolbox or AVFoundation frameworks, which contain routines for playing and mixing different types of sound.


The build target for your application.

1.4.4. Prototypes

Throughout this book, you'll be given a list of prototypes to check out at the end of most sections. Prototypes are header files that contain a list of all supported methods and properties that are available to you as a developer. While this book covers a majority of the methods and properties available, reading the prototypes on your own might reveal additional, and sometimes obscure interfaces that may have either gone undocumented or been added since this book's release. They'll also show you what arguments a method expects, and what data types will be returned.

You can find header files within a framework folder's Headers subfolder. Because there are different versions of the SDK, the exact path these can be found differs slightly. The standard format for the path to these prototypes is:


The full path relies on two variables, PLATFORM and VERSION. There are two primary platforms in the SDK: iPhoneOS.platform and iPhoneSimulator.platform. You use the former when building an application for an iPhone or iPod Touch device, and the simulator platform when you build an application for the iPhone simulator. Each platform contains its own set of frameworks, shared libraries, and prototypes.

The VERSION variable refers to the version of the iPhone SDK for that platform. The version is prefixed by the platform name and suffixed with an .sdk extension.

The full path for an iPhone platform running the version 2.2 SDK would look like the example below:


To make it easy to find this directory, add the following to your .profile script, so that the SDK environment variable is set every time a new shell is created:

export PLATFORM=iPhoneOS
export VERSION=2.2
export SDK=/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs\

You'll then be able to change directory using the environment variable SDK:

$ cd ${SDK}

Within the folder at this path, you will find the individual frameworks available in the SDK, each with a Headers directory containing the prototypes for that framework. Be sure to check these out when prompted, as they contain a great wealth of verbose information about what's available to developers. The general rule of thumb is this: if you find a class, property, or method in the SDK headers, it should be sanctioned for use in your application.


While a framework's headers will tell you which APIs you can use, they will not necessarily tell you in what way they can be used. Apple maintains a set of human interface guidelines and other policies that govern AppStore software. You'll need to be sure your application doesn't violate any of Apple's nontechnical restrictions, such as duplicating functionality of an existing preloaded application. Consult the latest version of your SDK agreement and other supporting documents from Apple to learn about design restrictions.

1.4.5. Adding Frameworks

For a given type of functionality, all of the classes and methods needed to provide it are grouped into a framework. For example, the Core Location framework provides all of the functionality needed to perform global positioning. The UI Kit framework provides all of the functionality needed to implement user interfaces. You'll need to link to a framework in order to use the functionality it provides. This design logically separates the different pieces of the iPhone's operating system for developers, and allows your application to link only to the components it needs.


When you link a framework with your application, its classes, functions, and symbols are made available to your application as if you wrote them yourself. Static linking compiles the objects directly into your application. Dynamic linking loads the objects at runtime. Adding a framework in the following fashion uses the dynamic method of linking.

Throughout this book, you may be prompted to add one or two frameworks to your example in order to add support for a particular type of functionality. To do this, Right-click the Frameworks folder in your Xcode project and select Addrightwards double arrowExisting Frameworks, as shown in Figure 1-5. Navigate to the iPhone SDK's Frameworks directory and choose the correct framework folder. Upon clicking the Add button, you should see the new framework appear in the Frameworks folder of your project. This will link the framework with your application.


It may be necessary to navigate to the Frameworks folder within your SDK. Use the pathnames you've just learned about for prototypes to find them.

Figure 1-5. Adding an existing framework in Xcode

1.4.6. Setting the Active SDK

Xcode allows you to build your application for either a physical iPhone device or for the integrated iPhone simulator. Different versions of the SDK may also be used to accommodate a specific version of the iPhone firmware. To switch between device and simulator builds, or to change the SDK version, click the Project menu in Xcode. Scroll down to the menu item labeled Set Active SDK and choose the SDK you want to build for. You can also choose whether to build with or without debugging support by choosing from the menu labeled Set Active Build Configuration. Building for the Debug configuration will allow you to trace your application's program execution to identify potential problems.

1.4.7. Building an Application

There are two ways to build an application using Xcode: the GUI and the command line. The easiest way, of course, is to simply click the Build button at the top of the project window or to click Build and Go to build and run the application. This will invoke the compiler and output the results of the build into the status bar. If the build fails or has warnings, you'll be able to click the appropriate icons on the status bar to get more information.

If you come from a Unix background, or just have an affinity for pain, you may be more comfortable building on the command line, especially if you use a command-line text editor in lieu of the Xcode IDE. To build from the command line, use the xcodebuild command:

$ xcodebuild -target Project_Name

1.4.8. Installing an Application

Once you have built an application, you can install it on your iPhone directly through Xcode. As mentioned earlier, this requires a valid developer key from Apple. When the application is installed, it will immediately appear on the iPhone's home screen, then be launched by the debugger.

To install from within Xcode, use the Build and Go toolbar button. This will recompile any changes made since the last build and install it onto the iPhone. If you don't have a developer key, and are building for the iPhone simulator platform, the application will be installed in the iPhone simulator and then run.

To install from the command line, use the xcodebuild command with the install build option:

$ xcodebuild install -target Project_Name