iOS Reference Library Apple Developer
Search

iOS Development Quick Start

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.

Essential Development Tasks

The iOS-application development process is divided into these major steps:

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

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

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

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

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

Creating an iOS-Application Project

The iPhone SDK provides several project templates to get you up and running developing your application. You can choose from these types of application:

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:

  1. Open the static-library target’s Info window and display the Build pane.

  2. In the Code Signing group, select the Any iPhone OS Device conditional definition for the Code Signing Identity build setting.

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

Figure 1-1  Project window

The project window identifying the toolbar, favorites bar, Groups & Files list, status bar, and detail view.

The project window contains the following key areas for navigating your project:

To learn more about creating projects, see “Creating Projects”.

Editing Code

The main tool you use to write your code is the Xcode text editor. This advanced text editor provides several convenient features:

For details about these and other text editor features, see “The Text Editor”.

Using Code Completion

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:

  1. Open the Xcode Preferences window.

    Choose Xcode > Preferences.

  2. In the Code Completion section of the Code Sense pane, choose Immediate from the Automatically Suggest pop-up menu.

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

Figure 1-2  Using code completion

Example of code completion in action. Text entered “CGPointM”, completion shown: “ake(CGFloat x, CGFloat y).

To learn more about code completion, see “Completing Code” in Xcode Workspace Guide.

Accessing Documentation

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.

Figure 1-3  Viewing API reference in the Documentation window

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.

Figure 1-4  Viewing API reference in the Research Assistant

Text editor with the cursor over the text “UIFont systemFontOfSize:(CGFloat)fontSize”. Research Assistant with API reference for the systemFontOfSize: method of the UIFont class.Text editor with the cursor over the text “UIFont systemFontOfSize:(CGFloat)fontSize”. Research Assistant with API reference for the systemFontOfSize: method of the UIFont class.

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.

Building and Running Your Application

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:

Xcode performs these tasks for you when you execute the Build command. See “Running Applications” for details.

Measuring Application Performance

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.

Further Exploration

To learn more about the Xcode development process, see A Tour of Xcode.

Tutorial: Hello, World!

This tutorial guides you through the creation of a simple project that prints text on the iPhone screen.

Create the Project

To create the Hello World project, follow these steps:

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

  2. Choose File > New Project.

  3. Select the Window-Based Application template, choose iPhone from the Product pop-up menu, and click Choose.

    New Project dialog with iPhone OS Application template group selected and six iPhone project templates shown. The Window-based–Application template is selected, with “Use Core Data for storage” option selected. A description of the template is also displayed.
  4. Name the project HelloWorld and choose a location for it in your file system.

  5. Add the MyView class to the project.

    1. Choose File > New File.

    2. Select the Cocoa Touch UIView subclass template and click Next.

      New File dialog with iPhone OS Cocoa Touch Class template group selected, and the Objective-C–class template selecteds. The options of the “Subclass of” pop-up menu are displayed; the UIView option is highlighted.
    3. In the File Name text field, enter MyView.m.

    4. Select the “Also create "MyView.h"” option and click Finish.

  6. Choose the active SDK for your project and the destination for the application.

    To set the active SDK:

    1. Choose Project > Edit Project Settings.

    2. Choose the SDK from the Base SDK for All Configurations pop-up menu.

      image: ../art/project_info-base_sdk.jpg

    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.

    Project window with Overview toolbar item disclosed showing the “active” settings for the project.Project window with Overview toolbar item disclosed showing the “active” settings for the project.

Write the Code

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:

  1. In the Groups & Files list, select the HelloWorld project.

  2. In the detail view, double-click HelloWorldAppDelegate.m.

  3. In the HelloWorldAppDelegate editor window:

    1. Add the following code line below the existing #import line.

      #import "MyView.h"
    2. 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:

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

  2. Jump to the second parameter and type 20.

  3. Enter the semicolon (;) at the end of the line and press Return.

Run the Application

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

iPhone Simulator with the “Hello, World!” white text on a black background on its screen.

Troubleshooting Hello, World! Build Errors

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

Further Exploration

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

Did this document help you? Yes It's good, but... Not helpful...