Read an Excerpt
Beginning iOS 5 Application Development
By Wei-Meng Lee
John Wiley & SonsCopyright © 2012 John Wiley & Sons, Ltd
All right reserved.
Chapter OneGetting Started with iOS 5 Programming
WHAT YOU WILL LEARN IN THIS CHAPTER
* How to obtain the iOS SDK - Software Development Kit
* Components included in the iOS SDK
* Features of the development tools — Xcode, Interface Builder, 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 App Store.
As the Chinese adage says, "To accomplish your mission, first sharpen your tools." Successful programming requires that you first 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.
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 Mac App Store (see 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 accelerometer 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).
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
* Dashcode — 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
* Instruments — 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.
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 Spotlight: Simply type Xcode into the search box and Xcode should be in the Top Hit position.
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.
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.
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.
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 specific versions of the OS.
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).
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
* Support for gestures:
* Touch and Hold
* 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 messages
* Installing applications from the App Store
* Several features of OpenGL ES
NOTE In the latest release of the SDK (5.0), the iOS Simulator enables you to simulate different 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 definitely 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 file 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 <iPhoneUserDomain>.
All third-party applications are stored in the <iPhoneUserDomain>/<version_no>/Applications/ folder. When an application is deployed onto the iOS Simulator, an icon is created on the Home screen and a file and a few folders are created within the Applications folder (see 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 next 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 confirm the deletion.
WARNING When an application is uninstalled, the corresponding file 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 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.
The Instruments application (see Figure 1-12) enables you to dynamically trace and profile the performance of your Mac OS X, iPhone, and iPad applications.
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 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, at http://developer .apple.com/mac/library/documentation/DeveloperTools/Conceptual/ InstrumentsUserGuide/Introduction/Introduction.html.
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 Cocoa Touch for the iPhone, rather than 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 the following:
* OS X Kernel
* Mach 3.0
* Power Management
* File System
The Core Services 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:
* Address Book
* File Access
* Core Location
* Net Services
* URL Utilities
The Media layer provides multimedia services that you can use in your iPhone and iPad applications. It consists of the following components:
* Core Audio
* Audio Mixing
* Audio Recording
* Video Playback
* JPG, PNG, TIFF
* Core Animation
* 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
* View Hierarchy
* Web Views
* People Picker
* Image Picker
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 specific functionalities. Refer to Apple's documentation at http://developer.apple.com/ iphone/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 fifth 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 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
* 3.1 — Additional features and bug fixes 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 iPhones.
* 5.0 — Fifth major release of the iOS. Supports new features like iCloud, iMessage, Twitter integration, Notification Center, etc.
For a detailed description of the features in each release, see http://en.wikipedia.org/wiki/ IPhone_OS_version_history.
Testing on Real Devices
One of the most common complaints about developing applications for the iPhone and iPad is how difficult 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 certificate; and for testing purposes, a developer certificate is required.
To test your applications on a device, you must sign up for the iOS Developer Program and request that a developer certificate be installed onto your device. Appendix A outlines these steps in detail.
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 x 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 x 640 pixels. Of course, you can turn off the status bar programmatically to gain access to the full 960 x 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 specifies 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 by the OS, which is very useful because it enables older applications to run and scale correctly without modifications 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.
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 first iOS application, and soon be on your way to iOS nirvana!
Excerpted from Beginning iOS 5 Application Development by Wei-Meng Lee Copyright © 2012 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. 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.