Developing iOS applications is a pleasant and rewarding endeavor. To convert your ideas into products you use Xcode, the integrated development environment (IDE) used to develop iOS applications. With Xcode you organize and edit your source files, view documentation, build your application, debug your code, and optimize your application’s performance.
Note: To develop iOS applications, you must be a registered Apple developer. To run applications on a device, you must be a member of the iPhone Developer Program. For more information, see “Managing Devices and Digital Identities.”
This chapter provides an overview of the major development tasks you follow to design, build, and run an application using Xcode. It also includes a quick tutorial that shows how to develop the ubiquitous Hello, World! application for iOS.
The iOS-application development process is divided into these major steps:
Create your project.
Xcode provides several project templates that get you started. You choose the template that implements the type of application you want to develop. See “Creating an iOS-Application Project” for details.
Design the user interface.
The Interface Builder application lets you design your application’s user interface graphically and save those designs as resource files that you load into your application at runtime. If you do not want to use Interface Builder, you can layout your user interface programmatically. See “User Interface Design Considerations” in iOS Application Programming Guide for more information.
Write code.
Xcode provides several features that help you write code fast, including class and data modeling, code completion, direct access to documentation, and refactoring. See “Editing Code” for details.
Build and run your application.
You build your application on your computer and run it in the iPhone Simulator application or on your device. See “Building and Running Your Application” for more information.
Measure and tune application performance.
After you have a running application, you should measure its performance to ensure that it uses a device’s resources as efficiently as possible and that it provides adequate responses to the user’s gestures. See “Measuring Application Performance” for more information.
The rest of this section gives more details about these steps.
The iPhone SDK provides several project templates to get you up and running developing your application. You can choose from these types of application:
Navigation-based Application. An application that presents data hierarchically, using multiple screens. The Contacts application is an example of a navigation-based application.
OpenGL ES Application. An application that uses an OpenGL ES–based view to present images or animation.
Split View–based Application. An iPad application that displays more than one view onscreen at a time to, for example, present data in a master-detail or source list–style arrangement. The iPad Mail application is an example of a split-view–based application.
Tab Bar Application. An application that presents a radio interface that lets the user choose from several screens. The Clock application is an example of a tab bar application.
Utility Application. An application that implements a main view and lets the user access a flip-side view to perform simple customizations. The Stocks application is an example of a utility application.
View-based Application. An application that uses a single view to implement its user interface.
Window-based Application. This template serves as a starting point for any application, containing an application delegate and a window. Use this template when you want to implement your own view hierarchy.
If you need to develop a static library for use in an iOS application, you can add a static library target to your project by choosing Project > New Target and selecting the Static Library target template in the iPhone OS/Cocoa Touch list.
Static libraries used in iOS applications do not need to be code signed. Therefore, you should remove the Code Signing Identity build setting definition from the static library targets you create. To do so:
Open the static-library target’s Info window and display the Build pane.
In the Code Signing group, select the Any iPhone OS Device conditional definition for the Code Signing Identity build setting.
Change the conditional definition’s value from iPhone Developer to Don’t Code Sign.
To learn more about the iOS application architecture, see iOS Application Programming Guide.
To develop an iOS application, you work on an Xcode project. And you do most of your work on projects through the project window, which displays and organizes your source files and other resources needed to build your application. This window allows you to access and edit all the pieces of your project. Figure 1-1 shows the project window.
The project window contains the following key areas for navigating your project:
Groups & Files list. Provides an outline view of your project’ contents. You can move files and folders around and organize your project contents in this list. The current selection in the Groups & Files list controls the contents displayed in the detail view.
Detail view. Shows the item or items selected in the Groups & Files list. You can browse your project’s contents in the detail view, search them using the search field, or sort them according to column. The detail view helps you rapidly find and access your project’s contents.
Toolbar. Provides quick access to the most common Xcode commands.
Favorites bar. Lets you store and quickly return to commonly accessed locations in your project. The favorites bar is not displayed by default. To display the favorites bar, choose View > Layout > Show Favorites Bar.
Status bar. Displays status messages for the project. During an operation—such as building or indexing—Xcode displays a progress indicator in the status bar to show the progress of the current task.
To learn more about creating projects, see “Creating Projects”.
The main tool you use to write your code is the Xcode text editor. This advanced text editor provides several convenient features:
Header-file lookup. By Command–double-clicking a symbol, you can view the header file that declares the symbol.
API reference lookup. By Option–double-clicking a symbol, you get access to API reference that provides information about the symbol’s usage.
Code completion. As you type code, you can have the editor help out by inserting text for you that completes the name of the symbol Xcode thinks you’re going to enter. Xcode does this in an unobtrusive and overridable manner.
Code folding. With code folding, you can collapse code that you’re not working on and display only the code that requires your attention.
For details about these and other text editor features, see “The Text Editor”.
The text editor helps you type code faster with code completion. When code completion is active, Xcode uses both text you have typed and the context into which you have typed it to provide suggestions for completing the token it thinks you intend to type. Code completion is not active by default.
To activate code completion:
Open the Xcode Preferences window.
Choose Xcode > Preferences.
In the Code Completion section of the Code Sense pane, choose Immediate from the Automatically Suggest pop-up menu.
Click OK.
As you type the name of a symbol, Xcode recognizes that symbol and offers a suggestion, as shown in Figure 1-2. You can accept suggestions by pressing Tab or Return. You may also display a list of completions by pressing Escape.
To learn more about code completion, see “Completing Code” in Xcode Workspace Guide.
During development, you may need fast access to reference for a particular symbol or high-level documentation about API usage or an iOS technology. Xcode gives you easy access to such resources through the Research Assistant and the Documentation window.
The Research Assistant is a lightweight window, shown in Figure 1-4, that provides a condensed view of the API reference for the selected item, without taking your focus away from the editor in which the item is located. This window provides an unobtrusive way to consult API reference. However, when you need to dig deeper into the reference, the Documentation window is just a click away.
The Documentation window (Figure 1-3) lets you browse and search the developer documentation (which includes API reference, guides, and articles about particular tools or technologies) installed on your computer. It provides access to a wider and more detailed view of the documentation than the Research Assistant, for the times when you need additional help.
To display the API reference for a symbol in a source file, you select the symbol in the text editor and choose Help > Find Selected Text in API Reference (you can also Option–double-click the symbol name). This command searches for the selected symbol in the API reference for your project’s SDK and displays it in the Documentation window. For example, if you select the UIFont
class name in a source file and execute the Find Selected Text in API Reference command, Xcode opens the Documentation window and displays the API reference for the UIFont
class.
While the Documentation window is a great tool to browse the iOS documentation library, sometimes you may not want to take your focus away from the text editor while you write code, but need basic information about a symbol in a condensed way. The Research Assistant provides such information in a small and unobtrusive window.
The Research Assistant actively follows you as you move the cursor around a source file. When it recognizes a symbol for which it finds API reference, the Research Assistant displays that reference, as shown in Figure 1-4. All you have to do is glance at the Research Assistant to get essential details about the symbol.
To display the Research Assistant, choose Help > Show Research Assistant.
From the Research Assistant you can quickly jump to more comprehensive reference for the symbol, or even view the header that declares it.
For more information about accessing documentation in Xcode, see Documentation Access.
iPhone Simulator implements the iOS API, providing an environment that closely resembles the environment devices provide. It allows you to run your applications in Mac OS X, letting you quickly test application functionality when you don’t have a device available. However, running applications in iPhone Simulator is not the same as running them in actual devices. iPhone Simulator does not emulate device performance: It doesn’t implement the memory constraints or processor performance of an actual device. First, the simulator uses Mac OS X versions of the low-level system frameworks instead of the versions that run on the devices. Secondly, there may be hardware-based functionality that’s unavailable on the simulator. But, in general, the simulator is a great tool to perform initial testing of your applications.
To get an accurate idea of how your application performs on a user’s device, you must run the application on a device and gather performance data using Instruments and other performance-measuring tools.
To compile and debug your code, Xcode relies on open-source tools, such as GCC and GDB. Xcode also supports team-based development with source control systems, such as Subversion, CVS, and Perforce.
Building your application involves the following steps:
Compiling your source files and generating your application binary.
Placing the binary in iPhone Simulator or on your device.
Xcode performs these tasks for you when you execute the Build command. See “Running Applications” for details.
After you have tested your application’s functionality, you must ensure that it performs well on a device. This means that the application uses the device’s resources as efficiently as possible. For example, memory is a scarce resource; therefore, your application should maintain a small memory footprint not to impair the performance of iOS. Your application should also use efficient algorithms to consume as little power as possible not to reduce battery life. Xcode provides two major tools to measure and tune application performance: Instruments and Shark.
The Instruments application is a dynamic performance analysis tool that lets you peer into your code as it’s running and gather important metrics about what it is doing. You can view and analyze the data Instruments collects in real time, or you can save that data and analyze it later. You can collect data about your application’s use of the CPU, memory, the file system, and the network, among other resources.
The Shark application is another tool that helps you find performance bottlenecks in your code. It produces profiles of hardware and software performance events and shows how your code works as a whole and its interaction with iOS.
See “Tuning Applications” for more information.
To learn more about the Xcode development process, see A Tour of Xcode.
This tutorial guides you through the creation of a simple project that prints text on the iPhone screen.
To create the Hello World project, follow these steps:
Launch the Xcode application, located in <Xcode>/Applications
.
<Xcode>
represents the directory in which you installed the Xcode toolset. See “Xcode Installation Details” for more information.
Choose File > New Project.
Select the Window-Based Application template, choose iPhone from the Product pop-up menu, and click Choose.
Name the project HelloWorld
and choose a location for it in your file system.
Add the MyView
class to the project.
Choose File > New File.
Select the Cocoa Touch UIView subclass template and click Next.
In the File Name text field, enter MyView.m
.
Select the “Also create "MyView.h"” option and click Finish.
Choose the active SDK for your project and the destination for the application.
To set the active SDK:
Choose Project > Edit Project Settings.
Choose the SDK from the Base SDK for All Configurations pop-up menu.
Note: If you have a development device plugged in, Xcode chooses the latest iPhone Device SDK for you. Otherwise, Xcode chooses the latest iPhone Simulator SDK.
To use an iPhone Device SDK you must be a member of the iPhone Developer Program. For more information, see “Becoming a Member of the iPhone Developer Program.”
The product destination specifies the type of environment for which Xcode builds your application and where to install it. If you have a development device plugged in at the time you create the project, Xcode sets the product destination to Device. Otherwise, it sets the destination to Simulator.
To set the product destination, choose between Device and Simulator from the Overview toolbar menu in the project window. If you have more than one device connected, specify the device onto which to install your application by choosing it from the Active Executable section of the Overview toolbar menu.
The Xcode text editor is where you spend most of your time. You can write code, build your application, and debug your code. Let’s see how Xcode assists you in writing code.
To experience the Xcode source-code editing features, you should perform the following instructions to enter the application’s source code. For your convenience, “Hello, World! Source Code” includes the final source code.
First, modify the HelloWorldAppDelegate
class to use the MyView
class:
In the Groups & Files list, select the HelloWorld project.
In the detail view, double-click HelloWorldAppDelegate.m
.
In the HelloWorldAppDelegate
editor window:
Add the following code line below the existing #import
line.
#import "MyView.h" |
Add the following code lines to the application:didFinishLaunchingWithOptions:
method, below the override-point comment.
MyView *view = [[MyView alloc] initWithFrame:[window frame]]; |
[window addSubview:view]; |
[view release]; |
After making these changes, the code in the HelloWorldAppDelegate.m
file should look similar to this:
#import "HelloWorldAppDelegate.h" |
#import "MyView.h" |
@implementation HelloWorldAppDelegate |
@synthesize window; |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
// Override point for customization after application launch |
MyView *view = [[MyView alloc] initWithFrame:[window frame]]; |
[window addSubview:view]; |
[view release]; |
[window makeKeyAndVisible]; |
return YES; |
} |
- (void)dealloc { |
[window release]; |
[super dealloc]; |
} |
@end |
Listing 1-1 shows the code that draws “Hello, World!” in the window. Add the highlighted code lines to the drawRect:
method in the MyView.m
file.
Listing 1-1 Method to draw “Hello, World!” in a view
- (void) drawRect:(CGRect) rect { |
NSString *hello = @"Hello, World!"; |
CGPoint location = CGPointMake(10, 20); |
UIFont *font = [UIFont systemFontOfSize:24.0]; |
[[UIColor whiteColor] set]; |
[hello drawAtPoint:location withFont:font]; |
} |
If you turned on code completion (as described in “Using Code Completion”), as you type symbol names the text editor suggests completions for the symbol names it recognizes. For example, as you type CGPointM
, the text editor suggests the completion shown in Figure 1-2. You can take advantage of completion here by accepting the suggested completion and jumping to the parameter placeholders:
Jump to the first parameter by choosing Edit > Select Next Placeholder, and type 10
.
The Select Next Placeholder command moves you among the arguments in function or method calls that the text editor suggests as completions to the text you’re typing.
Jump to the second parameter and type 20
.
Enter the semicolon (;
) at the end of the line and press Return.
To build and run the Hello World application, choose Build > Build and Run (or click the Build and Run toolbar item in the project window). If there are no build errors, Xcode installs the application in iPhone Simulator or your device (depending on the active SDK setting).
This section contains possible build errors for the Hello, World! project and their cause.
Building ... — 2 errors |
Compiling <project_directory>/Classes/HelloWorldAppDelegate.m (2 errors) |
error: 'MyView' undeclared (first use in this function) |
error: 'view' undeclared (first use in this function) |
Fix this build error by adding the line
#import "MyView.h"
to the HelloWorldAppDelegate.m
file.
To learn about other possible build errors, see “Solving Build Errors.”
Now that you learned how to write the standard Hello, World! application for iOS, you can experiment with HelloWorld, the Cocoa Touch version of this ubiquitous application.
For a step-by-step tutorial in developing a more complex application, see Your First iOS Application.
To learn more about Objective-C, see Learning Objective-C: A Primer.
To learn more about developing iOS applications, see iOS Application Programming Guide.
Last updated: 2010-07-02