Chapter 1. Building Your First iPhone App

You have an idea that will lead to fame and fortune on the iTunes App Store. You decide to write an iPhone app. The first and most important task is for you to become comfortable with the tools used to build your products. A Chinese proverb says, "the journey is the reward," and this chapter is all about the journey. In the upcoming pages, you'll experience the entire application development process, start to finish. You'll learn how to set up the software you need, and try your hand at building an app.

But what app? If you do a quick search of the App Store, you'll find no shortage of flashlights. For many aspiring developers, this simple application is a rite of passage, so now's your chance to join this illustrious crowd. Once you see how easy it is to create your own app, you'll wonder why people pay 99¢ for them on iTunes!

Getting the Tools

You can't build anything, including an iPhone app, without tools. Luckily, you can find everything you need on your Mac, or download it for free. Specifically, you need to download and install Xcode development software and the iPhone Software Development Kit (SDK) on your Mac. (And if you don't have a Mac, see the box on the next page.)

Both the Mac and iPhone benefit from a rich set of technologies that have stood the test of time. The iPhone SDK is built upon the infrastructure created by NeXT in the 1980s. This company, founded by Steve Jobs, created a revolutionary object-oriented operating system called NeXTSTEP. This influential system has evolved into the OS X operating system in use today. As you learn more about the iPhone, you'll see that it has much in common with the Mac.

Note

You see the NeXT legacy whenever you encounter an object with the prefix "NS". Those initials stand for NeXTSTEP.

Installing Xcode

Once you and your Mac are ready to go, it's time to load your hard drive with lots of new software. Apple supplies the Xcode development tools free of charge, but doesn't install them on every Mac, since most consumers will never use them.

Luckily, you can find the Xcode tools right on your Snow Leopard installation disk. To run Xcode, Apple recommends you have an Intel-based Mac running Leopard or Snow Leopard. The following steps explain how to get the software onto your hard drive where you can use it:

Note

You can install the iPhone SDK and other development tools on Leopard, but the Snow Leopard tools reflect significant improvements over the previous version. Working in the newest version of Mac OS X assures you the latest and greatest features.

  1. Pop the installation DVD into your Mac and double-click its icon. In the Optional Installs folder, double-click the Xcode.mpkg file.

    When you double-click that file, the Xcode installation process begins.

  2. On the introductory screen, click Continue. When the license agreement screen appears, click Continue and then click Agree.

    The license agreement is the same legalese you agree to whenever you install software. Read it if you're into such things. When you're done, the next screen lets you choose what you want to install, as shown in Figure 1-1.

    The Xcode installer starts out with all checkboxes except Mac OS X 10.4 selected for you. Leave them that way. You can click each package name to see what's being installed: Besides the integrated development environment (IDE) that you'd expect, you'll also find tools for monitoring performance and plenty of documentation.

    Figure 1-1. The Xcode installer starts out with all checkboxes except Mac OS X 10.4 selected for you. Leave them that way. You can click each package name to see what's being installed: Besides the integrated development environment (IDE) that you'd expect, you'll also find tools for monitoring performance and plenty of documentation.

  3. On the Custom Install screen, make sure all checkboxes except Mac OS X 10.4 are turned on, and then click Continue.

    The installer copies all of the Xcode from the DVD onto your hard drive. This process takes a few minutes.

  4. The final screen prompts you to select an install location. Make sure that it's on the same disk where all your other applications are stored. Click Install to start the process.

    Depending on your Mac's speed, this process can take a few hours. Get away from the computer and get some fresh air for once.

    After everything is safely on your hard drive, you see this message: "The installation was successful."

  5. Click Close to quit.

  6. You can safely eject the DVD at this point.

After the installation is complete, go to the Hard Drive→Developer→Applications folder on your hard drive, and check out your new tools. This folder contains the applications and utilities you use to develop both Mac and iPhone applications: The ones you'll use the most are Xcode and Interface Builder. The parent Developer folder also has all of the accompanying developer frameworks, libraries, and documentation.

The Xcode installation doesn't include one thing—the iPhone SDK that's required to develop apps for your phone. For that, go on to the next section.

Tip

Now that you have your tools, maintain them. Apple regularly updates Xcode, so the version on your Snow Leopard DVD will eventually become outdated. When major changes occur, Apple will send an email reminding you to upgrade by visiting the iPhone Dev Center, as described in the next section.

Getting the iPhone SDK

You have to join the iPhone Developer Program before Apple lets you get your hands on the iPhone SDK. Your free membership gives you access to the tools, documentation, and developer forums via the iPhone Dev Center (Figure 1-2).

The iPhone Dev Center is your first and best resource as an iPhone developer. You'll use this site to download and update your iPhone SDK, find sample code and documentation, connect with other iPhone developers, and to prepare your product for sale on iTunes.

Figure 1-2. The iPhone Dev Center is your first and best resource as an iPhone developer. You'll use this site to download and update your iPhone SDK, find sample code and documentation, connect with other iPhone developers, and to prepare your product for sale on iTunes.

  1. To sign up for an ADC membership, point your web browser to http://developer.apple.com/iphone/. Click the Register link in the upper-right corner.

    You access the iPhone Dev Center using an Apple ID. If you have an iTunes account or have made a purchase from the Apple Store, you already have one set up. Go ahead and use it when you create your developer account and skip to step 4.

    Note

    If you've been using your Apple ID for personal stuff like iTunes and a MobileMe family photo gallery, you may want to create a new Apple ID for your developer account. Having a separate Apple ID used solely for business purposes can help you avoid accounting and reporting issues. Please see Chapter 8 to see how your developer account and iTunes Connect affect your business.

  2. If you're setting up a new Apple ID, type your name, contact information, and security questions for password retrieval.

  3. Turn on the checkbox to accept the licensing agreement and click Continue.

    In a few minutes, Apple will send you an email to verify the account.

  4. Click the Email Verification link, and enter the code contained in the message to complete the account setup.

Once you set up your account and log in, you see a lot of new content available from the iPhone Dev Center. You have access to great resources like the Getting Started Videos, Coding How-To's, and Sample Code. Right now, turn your attention to the download for the iPhone SDK.

  1. Click the Downloads link, and you see a selection of links at the bottom of the page, as shown in Figure 1-3.

    As new versions of the iPhone SDK are released, these links will be updated. Pick the most recent release that matches your version of Mac OS X. At the time of this writing, it's "iPhone SDK 3.1.3 with Xcode 3.2.1".

    The iPhone SDK is a large download: Its size can range from several hundred megabytes to over 2 GB. Be patient as it downloads from your web browser, is verified, and mounted: It's going to take a while.

    Once it's finished, you have a .dmg disk image in your Downloads folder and a new iPhone SDK disk on your desktop, as shown in Figure 1-4.

You find the links to download the iPhone SDK toward the bottom of the iPhone Dev Center page. The links in this picture are for version 3.1.3, but these will change as Apple updates the SDK. You can click the Read Me links to see what's new in the release.

Figure 1-3. You find the links to download the iPhone SDK toward the bottom of the iPhone Dev Center page. The links in this picture are for version 3.1.3, but these will change as Apple updates the SDK. You can click the Read Me links to see what's new in the release.

After a successful download, this disk image appears on your desktop. Its name, which will vary with each new release, will begin with "iphone_sdk" followed by the version number and the ".dmg"extension. Launch the installer by double-clicking the box icon. The PDF file contains information about the release that you can read while the installation takes place.

Figure 1-4. After a successful download, this disk image appears on your desktop. Its name, which will vary with each new release, will begin with "iphone_sdk" followed by the version number and the ".dmg"extension. Launch the installer by double-clicking the box icon. The PDF file contains information about the release that you can read while the installation takes place.

Once you have the iPhone SDK disk image, you can begin the installation:

  1. Double-click the "iPhone SDK" file to start the installation process. It's the brown and gold box icon.

  2. Click Continue on the welcome and license agreement screens.

  3. Click Agree to accept the license.

  4. On the Install screen, click Continue to install the standard packages, and then click Install to start the installation process.

  5. If required, enter your password so system files can be modified.

  6. It's also a good idea to quit iTunes at this point to avoid a dialog box that pauses the install.

    Depending on your Mac's speed and the size of the download, the installation process can take anywhere from half an hour to several hours. When the installation is finished, you'll see a green checkmark and can click Close to finish. At this point you can eject the iPhone SDK disk, but keep the .dmg file around as a backup.

Note

As with Xcode, Apple updates the iPhone SDK regularly. You'll need to return to this iPhone Dev Center periodically to install the latest version of the SDK. Apple typically releases a new version in conjunction with a new iPhone firmware release.

What Lies Ahead for the SDK?

The iPhone SDK is constantly evolving as Apple fixes bugs and adds new features. You'll want to update your development environment to keep up with the latest changes. Apple updates the iPhone SDK in two different ways. The first, and simplest, is a maintenance release. These releases just fix bugs in the firmware and don't introduce any new features. In most cases, you won't need to make any changes to your application.

Apple provides maintenance releases of the SDK to developers on the same day that it makes the firmware available to customers. These releases have a three-part version number like 2.2.1 and 3.1.3. As soon as you install the new firmware on your device, you need to update the iPhone SDK so you can install and debug your applications from Xcode. If you don't, you'll see warnings that the tools don't support the device's firmware version.

When Apple makes more substantial firmware changes that will affect developer software, either by adding new features or changing existing ones, it posts a beta version of the iPhone SDK on the iPhone Dev Center. Only developers who have paid to join the iPhone Developer Program have access to these advance releases. (Objective-C: The Nuts and Bolts for Your iPhone App explains how to enroll in this developer program.) These betas are for major releases, such as 3.0 or 4.0, or revisions like 3.1. Apple typically starts the beta release cycle three or four months prior to a general public release. Once the cycle starts, it puts out a new SDK (called Beta 1, Beta 2, and so on) every couple of weeks. These beta releases usually also include a new version of Xcode with improvements and support for the new iPhone OS, along with new firmware.

With early access to the new SDK, you can build and run your application with the new iPhone firmware. If you've been careful to use only documented features and APIs, you shouldn't have many issues to deal with: Apple's engineers are very good at maintaining compatibility with published interfaces. You may see deprecation warnings as you compile, but those are usually simple to fix. It's more likely that you'll spend the beta test period learning about new features and testing them out in your application.

There are a couple caveats to keep in mind when installing a beta version of the iPhone SDK. First, you can't use the beta tools to submit an application to the App Store. Luckily, you can install multiple versions of Xcode on your hard drive. To install the tools in a separate location, follow these steps:

  1. Quit the iPhone Simulator if it's running.

    If you skip this step, the installation process will hang indefinitely, and you'll need to quit the Installer and start over.

  2. Double-click the iPhone SDK icon in the disk image to start the installation process. Agree to the licenses and choose a destination hard drive.

    You see a list of packages to install. In the second column, Developer is set as the Location. You need to change the location for the beta release.

  3. Click Developer and then select Other from the pop-up menu (see Figure 1-5).

    A dialog box opens for you to select a folder.

    You can choose a custom installation location for the iPhone SDK. Since you can't use beta releases of the iPhone SDK to build your application for the App Store, you'll need to keep two versions of the tools on your hard drive. During the install process, click the Developer folder icon and select Other to choose the location for the beta version.

    Figure 1-5. You can choose a custom installation location for the iPhone SDK. Since you can't use beta releases of the iPhone SDK to build your application for the App Store, you'll need to keep two versions of the tools on your hard drive. During the install process, click the Developer folder icon and select Other to choose the location for the beta version.

  4. Navigate to the root of your hard drive by selecting its name from the list of DEVICES. Then click the New Folder button and type DeveloperBeta. Click Create to create the folder.

  5. Select Choose to use the DeveloperBeta folder for the installation.

    After you return to the main installation window, you'll see DeveloperBeta as the Location.

  6. To use the beta, launch Xcode and other tools from the new Hard Drive→DeveloperBeta→Applications folder.

Now for the second caveat: the beta release is Apple Confidential Information and is covered by a Non-Disclosure Agreement (NDA). These big legal words mean that you can't talk about it in public. You can discuss the new SDK only on the Apple Developer Forums (http://devforums.apple.com). You can connect with other developers who are doing the same thing you are: learning about a new release by asking questions and sharing discoveries. Apple engineers also contribute to the discussion.

The NDA also means that you won't find any books or other media to help you understand the changes. The only information about the beta release comes from Apple itself and is posted on the iPhone Dev Center. Typically there's a "What's New" document, release notes, and a list of API differences. Read each of these documents fully: it's a great way to pass the time when you're waiting for several gigabytes of SDK to download!

Another source of information is Apple's annual developer conference, WWDC. Beta releases often coincide with this weeklong conference so everyone can discuss new features in detail. The conference takes place during the summer in San Francisco: it's a great opportunity to meet your fellow developers and learn lots of new things.

Get iPhone App Development: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.