Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

Beginning iPhone SDK: Programming with Objective-C

Beginning iPhone SDK: Programming with Objective-C

by Wei-Meng Lee

Everything you need to know to start creating native applications for the iPhone and iPod Touch

The iPhone SDK and the Xcode tools are the official Apple tools used for creating native iPhone applications. This information-packed book presents a complete introduction to the iPhone SDK and the Xcode tools, as well as the Objective-C language that is


Everything you need to know to start creating native applications for the iPhone and iPod Touch

The iPhone SDK and the Xcode tools are the official Apple tools used for creating native iPhone applications. This information-packed book presents a complete introduction to the iPhone SDK and the Xcode tools, as well as the Objective-C language that is necessary to create these native applications. Solid coverage and real-world examples walk you through the process for developing mobile applications for the iPhone that can then be distributed through Apple's iTunes Application store.

The hands-on approach shows you how to develop your first iPhone application while getting you acquainted with the iPhone SDK and the array of Xcode tools. A thorough tutorial on the features and syntax of the Objective-C language helps you get the most out of the iPhone SDK, and an in-depth look at the features of the iPhone SDK enables you to maximize each of these features in your applications.

  • Provides an introductory look at how the iPhone SDK and Xcode tools work with the Objective-C language to create native iPhone applications
  • Familiarizes you with the latest version of the iPhone SDK and the newest Xcode tools that ship with Snow Leopard
  • Walks you through developing your first iPhone applications
  • Focuses on the features and syntax of the Objective-C language so that you can get the most out of the iPhone SDK

With this hands-on guide, you'll quickly get started developing applications for the iPhone with both the iPhone SDK and the latest Xcode tools.

Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file.

Product Details

Publication date:
Product dimensions:
7.40(w) x 9.20(h) x 1.10(d)

Read an Excerpt

Beginning iPhone SDK Programming with Objective-C

By Wei-Meng Lee

John Wiley & Sons

Copyright © 2010 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-50097-2

Chapter One

Getting Started with iPhone Programming


* How to obtain the iPhone SDK

* The components included in the iPhone SDK

* The Features of the development tools - Xcode, Interface Builder, iPhone Simulator

* The Capabilities of the iPhone Simulator

* The Architecture of the iPhone OS

* The Frameworks of the iPhone SDK

* The Limitations and characteristics of the iPhone

Welcome to the world of iPhone programming! That you are now holding this book shows that you are fascinated with the idea of developing your iPhone applications and want to join the ranks of those tens of thousands of developers whose applications are already deployed in the AppStore.

As the old Chinese adage says, "To accomplish your mission, first sharpen your tools." Successful programming requires you first of all to know your tools well. Indeed, this couldn't be more true for iPhone programming - you need to know quite a few tools before you can even get started. Hence, the goal of this chapter is to show you the various relevant tools and information you need to jump on the iPhone development bandwagon.

Without further ado, it's time to get down to work.


To develop for the iPhone or iPod Touch, you first need to sign up as a Registered iPhone Developer at http://developer.apple.com/iphone/program/start/register/. The registration is free and provides you with access to the iPhone SDK and other resources that are useful for getting started.

After signing up, you can download the iPhone SDK (see Figure 1-1).

Before you install the iPhone SDK, make sure you satisfy the following system requirements:

* Only Intel Macs are supported, so if you have another processor type (such as the older G4 or G5 Macs), you're out of luck.

* You have updated your system with the latest Mac OS X release.

An iPhone/iPod Touch device is highly recommended, though not strictly necessary. To test your application, you can use the included iPhone Simulator. However, if you want to test certain hardware features like the camera, accelerometer, and such, you would need to use a real device.

When the SDK is downloaded, proceed with installing it (see Figure 1-2). You will be asked to accept a few licensing agreements and then select the destination folder in which to install the SDK.

If you have selected the default settings during the installation phase, after the installation you should be able to find the various tools installed in the /Developer/Applications folder (see Figure 1-3).


The iPhone SDK includes a suite of development tools to help you develop applications for your iPhone and iPod Touch. It includes:

* Xcode - the Integrated Development Environment (IDE) that allows you to manage, edit, and debug your projects.

* Dashcode - the Integrated Development Environment (IDE) that allows you to develop web-based iPhone applications and Dashboard Widgets. Dashcode is beyond the scope of this book.

* iPhone Simulator - provides a software simulator to simulate an iPhone on your Mac.

* Interface Builder - provides a visual editor for designing your user interfaces for your iPhone applications.

* Instruments - analysis tool to help you optimize your application in real-time.

The following sections discuss each tool in more detail.


As mentioned, all the tools in the iPhone SDK are installed in the /Developer/Applications folder (when using the default settings). One of those tools is Xcode.

To launch Xcode, double-click the Xcode icon (refer to Figure 1-3). Alternatively, go the quicker route and use Spotlight: Simply type Xcode into the search box and Xcode should be in the Top Hit position.

After Xcode is launched, you see the Welcome screen, shown in Figure 1-4.

Using Xcode, you can develop different types of iPhone and Mac OS X applications (see Figure 1-5).

The IDE in Xcode provides many tools and features that make your development life much easier. One such feature is Code Completion (see Figure 1-6), which displays a pop-up list showing the available classes and members (such as methods, properties, and so on).

NOTE For a more comprehensive description of some of the most commonly used features in Xcode, refer to Appendix B.

iPhone Simulator

The iPhone Simulator (see Figure 1-7) is a very useful tool included in the iPhone SDK that you can use to test your application without using your actual iPhone/iPod Touch. The iPhone Simulator is located in the /Developer/iPhone OS /Platforms/ iPhoneSimulator.platform/Developer/Applications/ folder. Most of the time, you don't need to launch the iPhone Simulator directly - running (or debugging) your application in Xcode automatically brings up the iPhone Simulator. Xcode installs the application on the iPhone Simulator automatically.

The iPhone Simulator can simulate different versions of the iPhone OS (see Figure 1-8). This capability is very useful if you need to support older versions of the platform as well as testing and debugging errors reported in the application on specific versions of the OS.

Features of the iPhone Simulator

The iPhone Simulator simulates various features of a real iPhone or iPod touch device. Features you can test on the iPhone Simulator include:

* Screen rotation - left, top, and right

* Support for gestures:

* Tap

* Touch and Hold

* Double Tap

* Swipe

* Flick

* Drag

* Pinch

* Low-memory warning simulations

However, the iPhone Simulator, being a software simulator for the real device, does have its limitations. Features not available on the iPhone Simulator include:

* Obtaining location data - it returns only a fixed coordinate, such as Latitude 37.3317 North and Longitude 122.0307 West

* Making phone calls

* Accessing the Accelerometer

* Sending and receiving SMS messages

* Installing applications from the App Store

* Camera

* Microphone

* Several features of OpenGL ES

It is worth noting that the speed of the iPhone Simulator is more tightly coupled to the performance of your Mac, as opposed to how the actual device performs. Hence, it is important that you test your application on a real device rather than rely exclusively on the iPhone Simulator for testing.

Although you have limitations with the iPhone Simulator, it is definitely a useful tool to get your applications tested. That said, testing your application on a real iPhone or iPod touch device is imperative before you deploy it on the AppStore.

NOTE For a more detailed look at how you can test your application on a real device, refer to Appendix E.

Uninstalling Applications from the iPhone Simulator

The user domain of the iPhone OS file system for the iPhone Simulator is stored in the ~/Library/ Application Support/iPhone Simulator/User/ folder.

NOTE The ~/Library/Application Support/iPhone Simulator/User/ folder is also known as the .

All third-party applications are stored in the /Applications/ folder. When an application is deployed onto the iPhone Simulator, an icon is created on the Home screen (shown on the left in Figure 1-9) and a file and a folder are created within the Applications folder (shown on the right in Figure 1-9).

To uninstall (delete) an application, execute the following steps:

1. Click and hold the icon of the application in the Home screen until all the icons start wriggling. Observe that all the icons now have an "x" button displayed on their top left corners.

2. Click the x button (see Figure 1-10) next to the icon of the application you want to uninstall.

3. An alert window appears asking if you are sure you want to delete the icon. Click Delete to confirm the deletion.

WARNING When the application is uninstalled, the corresponding file and folder in the Applications folder are deleted automatically.

To reset the iPhone Simulator to its original state, the easiest way is to select the iPhone Simulator [??] Reset Content and Settings menu item.

Interface Builder

Interface Builder is a visual tool that allows you to design your user interfaces for your iPhone applications. Using Interface Builder, you drag and drop views on windows and then connect the various views with outlets and actions so that they can programmatically interact with your code.

NOTE Outlets and actions are discussed in more detail in Chapter 3.

Figure 1-11 shows the various windows in Interface Builder.

Appendix C discusses Interface Builder in more detail.


The Instruments (see Figure 1-12) application allows you to dynamically trace and profile the performance of your Mac OS X and iPhone applications.

Using Instruments, you can:

* Stress test your applications

* Trace your applications for memory leaks

* Gain a deep understanding of the executing behavior of your applications

* Track difficult-to-reproduce problems in your applications

NOTE Covering the Instruments application is beyond the scope of this book. For more information, refer to Apple's documentation.


Although this book doesn't explore the innards of the iPhone OS, understanding some of the important points of the iPhone OS is useful. Figure 1-13 shows the different abstraction layers that make up the Mac OS X and the iPhone OS.

NOTE The iPhone OS is architecturally very similar to the Mac OS X except that the topmost layer is the Cocoa Touch for iPhone instead of the Cocoa Framework.

The bottom layer is the Core OS, which is the foundation of the operating system. It is in charge of memory management, the file system, networking, and other OS tasks, and it interacts directly with the hardware. The Core OS layer consists of components such as:

* OS X Kernel

* Mach 3.0


* Sockets

* Security

* Power Management

* Keychain

* Certificates

* File System

* Bonjour

The Core Services layer provides fundamental access to iPhone OS services. It provides an abstraction over the services provided in the Core OS layer. The Core Services layer consists of the following components:

* Collections

* Address Book

* Networking

* File Access

* SQLite

* Core Location

* Net Services

* Threading

* Preferences

* URL Utilities

The Media layer provides multimedia services that you can use in your iPhone applications. The Media layer consists of the following components:

* Core Audio

* OpenGL

* Audio Mixing

* Audio Recording

* Video Playback



* Quartz

* Core Animation

* OpenGL ES

The Cocoa Touch layer provides an abstraction layer to expose the various libraries for programming the iPhone and iPod Touch, such as:

* Multi-Touch events

* Multi-Touch controls

* Accelerometer

* View Hierarchy

* Localization

* Alerts

* Web Views

* People Picker

* Image Picker

* Controllers

The iPhone SDK consists of the following frameworks shown in Table 1-1, grouped by functionalities.

NOTE A framework is a software library that provides specific functionalities.


You now have a very good idea of the tools involved in iPhone application development. Before you go ahead and take the plunge, the following sections discuss some useful information that can make your journey a more pleasant one.

Versions of iPhone OS

At the time of writing, the iPhone OS is in its third revision - that is, version 3.0. The iPhone OS has gone through several revisions, and the major versions are as follows:

* 1.0 - initial release of iPhone

* 1.1 - additional features and bug fixes for 1.0

* 2.0 - released with iPhone 3G; comes with App Store

* 2.1 - additional features and bug fixes for 2.0

* 2.2 - additional features and bug fixes for 2.1

* 3.0 - third major release of the iPhone OS; see the next section for what is new in iPhone OS 3.0

For a detailed description of the features in each release, check out http://en.wikipedia .org/wiki/IPhone_OS_version_history.

Testing on Real Devices

One of the most common complaints that beginning iPhone programmers made was about the inability to test iPhone applications they have developed on their actual devices. It seems odd that as the owner of the device, they can't even test their applications on it. Turns out that for security reasons, Apple requires all applications to be signed with a valid certificate, and for testing purposes, a developer certificate is required.

To test your applications on a device, you must sign up for the iPhone Developer program and request that a developer certificate be installed onto your device. This is a lengthy process, but I give you detailed help with it in Appendix E.

Screen Resolution

The iPhone is a beautiful device with a high-resolution screen. At 3.5 inches (diagonally), the iPhone screen supports multitouch operation and allows a pixel resolution of 480 x 320 at 163 ppi (see Figure 1-14). When designing your application, note that despite the 480 x 320 resolution, most of the time you're limited to 460 x 320 pixels because of the status bar. Of course, you can turn off the status bar programmatically and gain access to the full 480 x 320 resolution.

Also, be mindful that users may rotate the device to display your application in Landscape mode. In that case, you have to make provisions to your user interface so that the applications can still work properly in Landscape mode should you decide to support the new orientation.

NOTE Chapter 6 discusses how to handle screen rotations.

Single-Window Applications

If you are new to mobile programming, you need to be aware that the limited screen real estate means that most mobile platforms support only single-window applications - that is, your application window occupies the entire screen. The iPhone is no exception to this platform limitation. Overlapping windows that are so common in desktop operating systems (such as Mac OS X and Windows) are not supported on the iPhone.

No Background Applications

One of the major challenges in programming mobile devices is power management. A badly written application can be a resource hog and will drain the battery of the device very quickly. Apple acknowledges this issue, and from reviewing experiences obtained from other platforms, decided that major culprits in hurting battery life and performance are background applications. On other platforms (such as Windows Mobile), when an application goes out of view (because of an incoming call, for example), the application remains in memory, and each background application retained in memory continues to takes its toll on the device's performance and battery life.

Apple's solution to this problem is simple: Disallow applications to run in the background. Although this is an effective solution, it has irked a lot of developers. Many useful applications require background operation to function correctly. For example, a chatting application needs to be running to receive messages from other users. To overcome this limitation, Apple has developed its Push Notification Service, which feeds applications with data even when they are not running. This service was released with iPhone 3.0. Using push technology, a device is constantly connected to Apple's server through an IP connection. When a device needs attention, a notification is sent from Apple's server to the device, thereby alerting the specific application that needs to service that notification.

NOTE Push notification is beyond the scope of this book. For more information, visit Apple's iPhone Dev Center at http://developer.apple.com/iphone/ index.action.

Restrictions on Calling Third-Party Applications

Another restriction that Apple places on iPhone developers is that you are not allowed to call third-party applications from within your application. In addition, you can't run interpreted code from within your application. A good example involves writing a Web browser application for the iPhone. Because Web applications typically use JavaScript for client-side interactivity, this restriction by Apple means that you can't run JavaScript code from within your application.


Excerpted from Beginning iPhone SDK Programming with Objective-C by Wei-Meng Lee Copyright © 2010 by John Wiley & Sons, Ltd. Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

Wei-Meng Lee is Founder of Developer Learning Solutions, a technology company specializing in hands-on training of the latest Microsoft and Apple technologies. He writes extensively for online publications and is the author of Professional Windows Vista Gadgets Programming and C# 2008 Programmer's Reference.

Customer Reviews

Average Review:

Post to your social network


Most Helpful Customer Reviews

See all customer reviews