Chapter 1. Getting Started with iPhone Programming

WHAT YOU WILL LEARN IN THIS CHAPTER

  • 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.

OBTAINING THE IPHONE SDK

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).

Figure 1-1

Figure 1.1. 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.

Figure 1-2

Figure 1.2. Figure 1-2

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).

Figure 1-3

Figure 1.3. Figure 1-3

COMPONENTS OF THE IPHONE SDK

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.

Xcode

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.

Figure 1-4

Figure 1.4. Figure 1-4

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

Figure 1-5

Figure 1.5. 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).

Figure 1-6

Figure 1.6. Figure 1-6

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 <version>/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.

Figure 1-7

Figure 1.7. Figure 1-7

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.

Figure 1-8

Figure 1.8. Figure 1-8

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 <iPhoneUserDomain>.

All third-party applications are stored in the <iPhoneUserDomain>/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).

Figure 1-9

Figure 1.9. 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.

    Figure 1-10

    Figure 1.10. Figure 1-10

  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

Figure 1-10

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.

Figure 1-11

Figure 1.11. Figure 1-11

Appendix C discusses Interface Builder in more detail.

Instruments

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

Figure 1-12

Figure 1.12. Figure 1-12

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.

ARCHITECTURE OF THE IPHONE OS

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.

Figure 1-13

Figure 1.13. Figure 1-13

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

  • BSD

  • 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

  • JPG, PNG, TIFF

  • PDF

  • 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.

Table 1.1. The Frameworks in the iPhone SDK

FRAMEWORK NAME

DESCRIPTION

AddressBook.framework

Provides access to the centralized database for storing a user's contacts.

AddressBookUI.framework

Provides the UI to display the contacts stored in the Address Book database.

AudioToolbox.framework

Provides low-level C APIs for audio recording and playback, as well as managing the audio hardware.

AudioUnit.framework

Provides the interface for iPhone OS-supplied audio processing plug-ins in your application.

AVFoundation.framework

Provides low-level C APIs for audio recording and playback, as well as for managing the audio hardware.

CFNetwork.framework

Provides access to network services and configurations, such as HTTP, FTP, and Bonjour services.

CoreAudio.framework

Declares data types and constants used by other Core Audio interfaces.

CoreData.framework

Provides a generalized solution for object graph management in your application.

CoreFoundation.framework

Provides abstraction for common data types, Unicode strings, XML, URL resource, and so on.

CoreGraphics.framework

Provides C-based APIs for 2D rendering; based on the Quartz drawing engine.

CoreLocation.framework

Provides location-based information using a combination of GPS, cell ID, and Wi-Fi networks.

ExternalAccessory.framework

Provides a way to communicate with accessories.

Foundation.framework

Provides the foundation classes for Objective C, such as NSObject, basic data types, operating system services, and so on.

GameKit.framework

Provides networking capabilities to games; commonly used for peer-to-peer connectivity and in-game voice feature.

IOKit.framework

Provides capabilities for driver development.

MapKit.framework

Provides an embedded map interface for your application.

MediaPlayer.framework

Provides facilities for playing movies and audio files.

MessageUI.framework

Provides a view-controller–based interface for composing e-mail messages.

MobileCoreServices.framework

Provides access to standard types and constants.

OpenAL.framework

Provides an implementation of the OpenAL specification.

OpenGLES.framework

Provides a compact and efficient subset of the OpenGL API for 2D and 3D drawing.

QuartzCore.framework

Provides ability to configure animations and effects and then render those effects in hardware.

Security.framework

Provides the ability to secure your data and control access to software.

StoreKit.framework

Provides in-app purchase support for applications.

SystemConfiguration.framework

Provides the ability to determine network availability and state on device.

UIKit.framework

Provides the fundamental objects for managing an application's UI.

SOME USEFUL INFORMATION BEFORE YOU GET STARTED

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 × 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 × 320 pixels because of the status bar. Of course, you can turn off the status bar programmatically and gain access to the full 480 × 320 resolution.

Figure 1-14

Figure 1.14. Figure 1-14

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.

SUMMARY

This chapter offered a quick tour of the tools used for iPhone application development. You also learned some of the characteristics of iPhone, such as the one-application limit and inability to call third-party applications. In the next chapter, you develop your first iPhone application, and you will soon be on your way to iPhone nirvana!

  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Obtaining the iPhone SDK

Register as an iPhone Developer at http://developer.apple.com first and download the free SDK.

iPhone Simulator

Most of the testing can be done on the iPhone Simulator. However, it is strongly recommended that you have a real device for actual testing.

Limitations of the iPhone Simulator

Access to hardware is generally not supported by the simulator. For example, the camera, accelerometer, voice recording, and so on are not supported.

Frameworks in the iPhone SDK

The iPhone SDK provides several frameworks that perform specific functionalities on the iPhone. You program your iPhone applications using all these frameworks.

Background applications

The iPhone does not support third-party background applications.

Screen resolution

480 × 320 pixels (with status bar hidden). 460 × 320 pixels (with status bar visible).

Single window applications

All iPhone applications are single-windowed — that is, all windows fill the entire screen and overlapping windows are not allowed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.250