1 Getting Started with iOS 5 Programming

WHAT YOU WILL LEARN IN THIS CHAPTER

➤ How to obtain the iOS SDK - Development Kit

➤ Components included in the iOS SDK

➤ Features of the development tools — , , and iOS Simulator

➤ Capabilities of the iOS Simulator

➤ Architecture of iOS

➤ Characteristics of the iPhone and iPad

Welcome to the world of iOS programming! That you are now holding this book shows that you are fascinated with the idea of developing your own iPhone and iPad applications and want to join the ranks of the tens of thousands of developers whose applications are already deployed in the . As the Chinese adageCOPYRIGHTED says, “To accomplish your mission, MATERIAL fi rst sharpen your tools.” Successful programming requires that you fi rst know your tools well. Indeed, this couldn’t be truer for iOS programming — you need to be familiar with quite a few tools before you can even get started. Hence, this chapter describes the various relevant tools and information you need to jump on the iOS development bandwagon. Without further ado, it’s time to get down to work.

cc01.indd01.indd 1 005/12/115/12/11 11:38:38 PPMM 2 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

OBTAINING THE TOOLS AND SDK

To develop for iOS, you need to download the iOS SDK. The iOS SDK comes with free Xcode from the (see Figure 1-1).

FIGURE 1-1

Before you download and install Xcode, 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.

➤ Your system is updated with the latest Mac OS X Lion release. An actual iPhone/iPod touch/iPad is highly recommended, although not strictly necessary. To test your application, you can use the included iOS Simulator (which enables you to simulate an iPhone or an iPad). However, to test certain hardware features like the and gyroscope, you need to use a real device. When Xcode is downloaded, proceed with installing it. Accept a few licensing agreements and then select the destination folder in which to install the SDK. If you select the default settings during the installation phase, the various tools will be installed in the /Developer/Applications folder (see Figure 1-2).

FIGURE 1-2

cc01.indd01.indd 2 005/12/115/12/11 11:38:38 PPMM Components of Xcode ❘ 3

COMPONENTS OF XCODE

The Xcode package includes a suite of development tools to help you create applications for your iPhone, iPod touch, and iPad. It includes the following:

➤ Xcode IDE — Integrated development environment (IDE) that enables you to manage, edit, and debug your projects

— Integrated development environment (IDE) that enables you to develop web-based iPhone and iPad applications and Dashboard widgets. Dashcode is beyond the scope of this book.

➤ iOS Simulator — Provides a software simulator to simulate an iPhone or an iPad on your Mac

➤ Interface Builder — Visual editor for designing user interfaces for your iPhone and iPad applications

— Analysis tool to help you both optimize your application and monitor for memory leaks in real time The following sections discuss each tool (except Dashcode) in more detail. Xcode To launch Xcode, double-click the Xcode icon located in the /Developer/Applications folder (refer to Figure 1-2). Alternatively, go the quicker route and use : Simply type Xcode into the search box and Xcode should be in the Top Hit position. Figure 1-3 shows the Xcode Welcome screen.

FIGURE 1-3

cc01.indd01.indd 3 005/12/115/12/11 11:38:38 PPMM 4 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

Using Xcode, you can develop different types of iPhone, iPad, and Mac OS X applications using the various project templates shown in Figure 1-4.

FIGURE 1-4

For iOS applications, each template gives you the option to select the platform you are targeting — iPhone, iPad, or Universal (runs on both iPhone and iPad). The IDE in Xcode provides many tools and features that make your development life much easier. One such feature is Code Sense, which displays a popup 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.

iOS Simulator The iOS Simulator, shown in Figure 1-5, is a very useful tool that you can use to test your application without using your actual iPhone/iPod touch/iPad. The iOS Simulator is located in the /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications folder. Most of the time, you don’t need to launch the iOS Simulator directly — running (or debugging) your application in Xcode automatically brings up the iOS Simulator. Xcode installs the application on the iOS Simulator automatically.

cc01.indd01.indd 4 005/12/115/12/11 11:38:38 PPMM Components of Xcode ❘ 5

FIGURE 1-5

THE IOS SIMULATOR IS NOT AN EMULATOR

To understand the difference between a simulator and an emulator, keep in mind that a simulator tries to mimic the behavior of a real device. In the case of the iOS Simulator, it simulates the real behavior of an actual iPhone/iPad device. However, the Simulator itself uses the various libraries installed on the Mac (such as QuickTime) to perform its rendering so that the effect looks the same as an actual iPhone. In addition, applications tested on the Simulator are compiled into x86 code, which is the byte-code understood by the Simulator. A real iPhone device, conversely, uses ARM-based code. In contrast, an emulator emulates the working of a real device. Applications tested on an emulator are compiled into the actual byte-code used by the real device. The emulator executes the application by translating the byte-code into a form that can be executed by the host computer running the emulator. To understand the subtle difference between simulation and emulation, imagine you are trying to convince a child that playing with knives is dangerous. To simulate this, you pretend to cut yourself with a knife and groan in pain. To emulate this, you actually cut yourself.

The iOS Simulator can simulate different versions of the iOS (see Figure 1-6. To support older versions of the SDK, you need to install the previous versions of the SDKs). This capability is useful if you need to support older versions of the platform, as well as test and debug errors reported in the application on specifi c versions of the OS.

cc01.indd01.indd 5 005/12/115/12/11 11:38:38 PPMM 6 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

FIGURE 1-6

In addition, the iOS Simulator can simulate different devices — iPad (see Figure 1-7), iPhone (3G and 3GS), and iPhone 4 with Retina display (see Figure 1-8).

FIGURE 1-7 FIGURE 1-8

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

➤ Screen rotation — left, right, top, and upside down

cc01.indd01.indd 6 005/12/115/12/11 11:38:38 PPMM Components of Xcode ❘ 7

➤ Support for gestures:

➤ Tap

➤ Touch and Hold

➤ Double-tap

➤ Swipe

➤ Rotate

➤ Drag

➤ Pinch

➤ Low-memory warning simulations However, the iOS Simulator, being a software simulator for the real device, does have its limitations. The following features are not available on the iOS Simulator:

➤ Making phone calls

➤ Accessing the accelerometer

➤ Sending and receiving SMS

➤ Installing applications from the App Store

➤ Camera

➤ Microphone

➤ Several features of OpenGL ES

NOTE In the latest release of the SDK (5.0), the iOS Simulator enables you to simulate diff erent locations as well as movements. Chapter 20 discusses this in more detail.

Note also that the speed of the iOS Simulator is more tightly coupled to the performance of your Mac than the actual device. Therefore, it is important that you test your application on a real device, rather than rely exclusively on the iOS Simulator for testing. Despite the iOS Simulator’s limitations, it is defi nitely a useful tool for testing your applications. That said, testing your application on a real device is imperative before you deploy it on the App Store.

Uninstalling Applications from the iOS Simulator The user domain of the iOS fi le system for the iOS Simulator is stored in the ~/Library/ Application Support/iPhone Simulator/ folder.

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

cc01.indd01.indd 7 005/12/115/12/11 11:38:38 PPMM 8 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

All third-party applications are stored in the //Applications/ folder. When an application is deployed onto the iOS Simulator, an icon is created on the Home screen and a fi le and a few folders are created within the Applications folder (see Figure 1-9).

FIGURE 1-9

To uninstall (delete) an application, execute the following steps: 1. Click and hold the icon of the application on the Home screen until all the icons start wriggling. Note that all the icons now have an X button displayed on their top-left corner. 2. Click the X button to the icon of the application you want to uninstall (see Figure 1-10). 3. An alert window appears asking if you are sure you want to delete the icon. Click Delete to confi rm the deletion.

FIGURE 1-10

cc01.indd01.indd 8 005/12/115/12/11 11:38:38 PPMM Components of Xcode ❘ 9

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

The easiest way to reset the iOS Simulator to its original state is to select iOS Simulator ➪ Reset Content and Settings . . . . Interface Builder Interface Builder is a visual tool that enables you to design the user interfaces for your iPhone/iPad applications. Using Interface Builder, you drag and drop views onto 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, and Appendix B discusses Interface Builder in more detail.

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

FIGURE 1-11

cc01.indd01.indd 9 005/12/115/12/11 11:38:38 PPMM 10 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

Instruments The Instruments application (see Figure 1-12) enables you to dynamically trace and profi le the performance of your Mac OS X, iPhone, and iPad applications.

FIGURE 1-12

Using Instruments, you can do all of the following:

➤ Stress test your applications.

➤ Monitor your applications for memory leaks.

➤ Gain a deep understanding of the executing behavior of your applications.

➤ Track diffi cult-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, at http://developer .apple.com/mac/library/documentation/DeveloperTools/Conceptual/ InstrumentsUserGuide/Introduction/Introduction.html.

cc01.indd01.indd 1100 005/12/115/12/11 11:38:38 PPMM Architecture of the iOS ❘ 11

ARCHITECTURE OF THE IOS

Although this book doesn’t explore the innards of iOS, understanding some of its important characteristics is useful. Figure 1-13 shows the different abstraction layers that make up the Mac OS X and iOS (which is used by the iPhone, iPod touch, and iPad).

NOTE The iOS is architecturally very similar to the Mac OS X except that the topmost layer is for the iPhone, rather than the Cocoa Framework.

FIGURE 1-13

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

➤ OS X Kernel

➤ Mach 3.0

➤ BSD

➤ Sockets

➤ Security

➤ Power Management

➤ Certifi cates

cc01.indd01.indd 1111 005/12/115/12/11 11:38:38 PPMM 12 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

The layer provides an abstraction over the services provided in the Core OS layer. It provides fundamental access to iOS services and 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 and iPad applications. It consists of the following components:

➤ OpenGL

➤ Audio Mixing

➤ Audio Recording

➤ Video Playback

➤ JPG, PNG, TIFF

➤ PDF

➤ OpenGL ES The Cocoa Touch layer provides an abstraction layer to expose the various libraries for programming the iPhone and iPad, such as the following:

➤ Multi-Touch events

➤ Multi-Touch controls

➤ Accelerometer

➤ View Hierarchy

➤ Localization

➤ Alerts

➤ Web Views

cc01.indd01.indd 1212 005/12/115/12/11 1:381:38 PMPM Some Useful Information before You Get Started ❘ 13

➤ People Picker

➤ Image Picker

➤ Controllers In iOS programming, all the functionalities in each layer are exposed through various frameworks that you will use in your project. Subsequent chapters in this book demonstrate how to use these frameworks in your projects.

NOTE A framework is a software library that provides specifi c functionalities. Refer to Apple’s documentation at http://developer.apple.com/ /library/documentation/Miscellaneous/Conceptual/ iPhoneOSTechOverview/iPhoneOSFrameworks/iPhoneOSFrameworks.html for a list of frameworks included in the SDK.

SOME USEFUL INFORMATION BEFORE YOU GET STARTED

You now have a good idea of the tools involved in iPhone and iPad application development. Before you go ahead and take the plunge, the following sections discuss some useful information that can make your journey more pleasant. Versions of iOS At the time of writing, iOS is in its fi fth revision — that is, version 5.0. Its major versions are as follows:

➤ 1.0 — Initial release of the iPhone

➤ 1.1 — Additional features and bug fi xes for 1.0

➤ 2.0 — Released with iPhone 3G; comes with App Store

➤ 2.1 — Additional features and bug fi xes for 2.0

➤ 2.2 — Additional features and bug fi xes for 2.1

➤ 3.0 — Third major release of the iPhone OS

➤ 3.1 — Additional features and bug fi xes for 3.0

➤ 3.2 — This version release is for the iPad only.

➤ 4.0 — Fourth major release of the iPhone OS. Renamed as iOS. This version is designed for the new iPhone 4 and it also supports older devices, such as the iPod touch and .

➤ 5.0 — Fifth major release of the iOS. Supports new features like iCloud, iMessage, Twitter integration, Notifi cation Center, etc. For a detailed description of the features in each release, see http://en.wikipedia.org/wiki/ IPhone_OS_version_history.

cc01.indd01.indd 1133 005/12/115/12/11 11:38:38 PPMM 14 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

Testing on Real Devices One of the most common complaints about developing applications for the iPhone and iPad is how diffi cult Apple makes it to test a new application on an actual device. Nonetheless, for security reasons, Apple requires all applications to be signed with a valid certifi cate; and for testing purposes, a developer certifi cate is required. To test your applications on a device, you must sign up for the iOS Developer Program and request that a developer certifi cate be installed onto your device. Appendix A outlines these steps in detail. Screen Resolutions The iPhone 4S is a beautiful device with a high-resolution screen. At 3.5 inches (diagonally), the iPhone screen supports multi-touch operation and allows a pixel resolution of 960 × 640 at 326 ppi (see Figure 1-14). When designing your application, note that because of the status bar, the actual resolution is generally limited to 920 × 640 pixels. Of course, you can turn off the status bar programmatically to gain access to the full 960 × 640 resolution. Also, be mindful that users may rotate the device to display your application in landscape mode. You need to make provisions to your user interface so that applications can still work properly in landscape mode.

NOTE Chapter 7 discusses how to handle screen rotations.

The older iPhones (iPhone 3G/3GS) and the iPod touch have lower resolutions compared to the iPhone 4/4S. They have a resolution of 480 × 320 pixels, one quarter of the resolution of the iPhone 4. When programming for the iPhones, it is important to note the difference between points and pixels. For example, the following statement specifi es a frame that starts from the point (20,10) with a width of 280 points and a height of 50 points:

CGRect frame = CGRectMake(20, 10, 280, 50);

On the older iPhones, a point corresponds to a pixel. Thus, the preceding statement translates directly to the pixel (20,10), with a width of 280 pixels and a height of 50 pixels. However, if the statement is executed within the iPhone 4/4S, a point translates to two pixels. Thus, the preceding statement translates into the pixel (40,20), with a width of 560 pixels and a height of 100 pixels. The translation is performed automatically FIGURE 1-14

cc01.indd01.indd 1144 005/12/115/12/11 11:38:38 PPMM Summary ❘ 15

by the OS, which is very useful because it enables older applications to run and scale correctly without modifi cations on the iPhone 4/4S. The iPad has a pixel resolution of 1,024 × 768 at 132 ppi. Table 1-1 summarizes the screen resolutions for the various platforms.

TABLE 1-1: Platform Resolutions

VISIBLE REAL ESTATE WITHOUT VISIBLE REAL ESTATE RESOLUTION THE STATUS BAR (PIXELS) — WITHOUT THE STATUS BAR PLATFORM (PIXELS) LANDSCAPE MODE (PIXELS) — PORTRAIT MODE

iPhone 4/4S 960 × 640 960 × 600 920 × 640

iPhone 480 × 320 480 × 300 460 × 320 3G/3GS, iPod touch

iPad 1024 × 768 1024 × 748 1004 × 768

SUMMARY

This chapter offered a quick tour of the available tools used for iPhone and iPad application development. You had a look at the iOS Simulator, which you will use to test your applications without using a real device. The Simulator is a very powerful tool that you will use very often in your iPhone development journey. You also learned some of the characteristics of the iPhone and iPad, such as screen resolutions, as well as characteristics of the operating systems. In the next chapter, you will develop your fi rst iOS application, and soon be on your way to iOS nirvana!

cc01.indd01.indd 1515 005/12/115/12/11 1:381:38 PMPM 16 ❘ CHAPTER 1 GETTING STARTED WITH IOS 5 PROGRAMMING

 WHAT YOU LEARNED IN THIS CHAPTER

TOPIC KEY CONCEPTS

Obtaining the iOS SDK Download Xcode 4 from the Mac App Store.

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

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

Frameworks in the The iOS SDK provides several frameworks that perform specifi c iOS SDK functionalities on the iPhone. You program your iOS applications using all these frameworks.

cc01.indd01.indd 1616 005/12/115/12/11 1:381:38 PMPM