Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / HTML5, CSS3, and JavaScript /

Getting started with PhoneGap in Xcode for iOS

by Andrew Trice

Andrew Trice
  • Adobe Systems, Inc.

Content

  • Setting up Xcode
  • Downloading and installing PhoneGap
  • Creating a project in Xcode
  • Working with PhoneGap in Xcode
  • Where to go from here

Created

27 February 2012

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
HTML5 iOS JavaScript mobile PhoneGap

Requirements

User level

All

This article describes steps needed to set up an environment and a workflow for developing PhoneGap/Apache Cordova applications in Xcode, targeting iOS devices. Xcode is the integrated development environment (IDE) provided by Apple for developing OS X and iOS applications. iOS is Apple's mobile operating system for iPhone, iPad, and iPod touch devices. PhoneGap is an open source application platform that enables you to create natively-installed mobile applications using HTML and JavaScript, also known as Apache Cordova.

Setting up Xcode

The first step to setting up Xcode for PhoneGap applications is to download and install the Xcode IDE from Apple. You can download Xcode from the Apple developer site. Before you can download Xcode, you must register as a developer with Apple. Once you do, you get access to Xcode, the iOS device simulator, and Apple developer documentation for free. If you want to deploy your applications to an actual device or distribute your applications via the iTunes store, you will have to register with Apple as an iOS developer, which has a yearly cost associated with it.

After you download Xcode, simply follow the setup wizard to complete the installation process.

Downloading and installing PhoneGap

Once you have successfully installed Xcode, your next step is to download and install PhoneGap.

  1. Visit the PhoneGap download page (http://phonegap.com/download) in your browser and click the orange Download link to start the download.

Note: The following steps are shown for PhoneGap 1.5.0, but the process should be applicable or similar to all versions of PhoneGap.

  1. Extract the ZIP file's contents.
  2. Navigate to the libs/ios/ folder of the extracted files, and double-click on the Cordova-1.5.0.pkg package installer.

This will launch the PhoneGap installation wizard, which will guide you through the process of installing PhoneGap on your system (see Figure 1).

Figure 1. The PhoneGap/Cordova installer.
Figure 1. The PhoneGap/Cordova installer.
  1. Follow the installation instructions provided by the installer until you see "Thanks for installing PhoneGap 1.5.0! ".

When you see this message, PhoneGap installation is complete. You are now ready to create your first PhoneGap project within Xcode.

Creating a project in Xcode

The PhoneGap installer adds a PhoneGap project template to Xcode, which streamlines the creation of PhoneGap applications.

  1. To get started, simply click Create A New Xcode Project in the Xcode welcome screen (see Figure 2). Alternatively, you can choose File > New > New Project to create your new PhoneGap project.
Figure 2. The Xcode welcome screen.
Figure 2. The Xcode welcome screen.

The first step in creating a new Xcode project is to select a project template.

  1. Find and select the Cordova-based Application template, located under iOS > Application, and then click Next (see Figure 3).
Figure 3. Choosing a project template.
Figure 3. Choosing a project template.

After you have chosen the project template, you'll need to name your project. Give it a name indicative of what you're going to be building.

  1. In this case just type Hello, since you're building a simple Hello World application (see Figure 4).
Figure 4. Setting the project name and other options.
Figure 4. Setting the project name and other options.

On the New Project Options screen, you also need to enter a company identifier. The company identifier is the prefix that is concatenated with the product name to create the unique bundle identifier for your application. The bundle identifier should match the App ID that you create for a unique application within the Apple Provisioning Portal (see Figure 5).

Figure 5. Specifying the bundle identifier in the Apple Provisioning Portal.
Figure 5. Specifying the bundle identifier in the Apple Provisioning Portal.

If you aren't sure what to type here, or if you are not deploying to a device or distributing this application right away, you can type whatever you want or leave this blank. You can always change the bundle identifier later by editing the Identifier field in the Xcode project summary screen, or by manually editing your project's Info.plist file.

Note: On the project options screen, you also have the option to turn on Automated Reference Counting (ARC). If you aren't writing any native code within your PhoneGap application, you can ignore this. You only need to worry about ARC if you are writing PhoneGap native plugins or writing native code. PhoneGap native plugins enable you to write your own native code and JavaScript APIs to extend the capabilities of the PhoneGap container.

PhoneGap projects will work regardless of whether ARC is enabled or disabled. If ARC is enabled, then you do not need to manage memory allocation using retain and release instructions in any native code. The Xcode LLVM compiler will automatically generate the appropriate retain and release instructions for you. If ARC is not enabled, you will have to manually manage memory. Remember, ARC only applies to native code, not HTML or JavaScript code.

  1. Click Next.
  2. Select a folder on your file system in which your PhoneGap project will reside (see Figure 6).
Figure 6. Selecting the project location.
Figure 6. Selecting the project location.
  1. Click Create.

Your PhoneGap project will be created within Xcode. However, there are a few more steps you need to complete before your project is up and running.

All of the HTML and JavaScript used to create the interface for your PhoneGap project will be contained within a www folder. Initially, this www folder will not exist within your PhoneGap project. To create it, you first have to run the application.

  1. Run the application by clicking Run in the upper left hand corner of the window (see Figure 7). Alternatively, you can choose Product > Run.
Figure 7. The Xcode Run button.
Figure 7. The Xcode Run button.

Xcode will attempt to launch the PhoneGap application in the iOS simulator. For the first launch, you will see the error message "ERROR: Start Page at 'www/index.html' was not found." Do not worry, this is normal! During this first step, the www folder will be created. After you add it to your project, you should not encounter this error again.

  1. To add the www folder to your project, Ctrl-click the project root and select Add Files To "Hello" (see Figure 8).
Figure 8. Preparing to add files to the project.
Figure 8. Preparing to add files to the project.
  1. In the dialog box that opens, select the www folder inside of the newly created project. Be sure to select the actual www folder, not a parent folder or contents inside of the folder.
  2. Select Create Folder References For Any Added Folders (see Figure 9). If you do not select this option, your will encounter errors with your project.
Figure 9. Adding files to the project in Xcode.
Figure 9. Adding files to the project in Xcode.
  1. Click Add.

You should now see the www folder under your project root (see Figure 10).

Figure 10. The www folder in the project root.
Figure 10. The www folder in the project root.
  1. Click Run to launch the project again.

At this point, your project should be correctly configured and should launch in the iOS simulator. If you haven't changed any of the HTML files inside of the www folder, you will see the default PhoneGap starter page inside of your application running within the iOS simulator (see Figure 11).

Figure 11. The iOS simulator
Figure 11. The iOS simulator

If you see this, then you are now ready to begin developing iOS applications within Xcode using PhoneGap.

Working with PhoneGap in Xcode

With your PhoneGap environment properly set up, reviewing some Xcode and PhoneGap basics will help you make the most of this development environment.

In general, Xcode should be fairly easy to navigate. On the left side of the screen you will see the project hierarchy, and in the center of the screen you will see the main content area. As you select files on the left side, the content will be displayed within the main editor area.

Here is one simple tip that trips up a lot of people: single-clicking a file in the project hierarchy displays that file's contents in the same window; double-clicking a file opens the file in a new window.

If you select the project root, you will see the project summary screen. From this screen, you can easily set the bundle identifier, application version, target devices, icons, splash screens, and supported orientations among other options (see Figure 12). This screen is useful in configuring your application for deployment.

Figure 12. The project summary screen.
Figure 12. The project summary screen.

At the top of the Xcode window, near the Run button, there is a Scheme menu that lets you change the project compilation schemes and target devices or simulators. To target a specific device or change the iOS Simulator target, simply click the project name and then select an alternate device or simulator target (see Figure 13). The next time that you run the application, it will be deployed to the newly selected target.

Figure 13. Selecting a new scheme.
Figure 13. Selecting a new scheme.

Keep in mind that the entire interface of a PhoneGap application is developed using HTML and JavaScript. Maintain all of the HTML assets used within the interface in the www folder. If you want to add additional files from a remote location, you will need to white-list that location inside of the PhoneGap.plist file, otherwise any external requests will be blocked with a security exception.

By default, the content area of Xcode will display source code based upon the files you select in the project hierarchy (see Figure 14).

Figure 14. The Xcode editor
Figure 14. The Xcode editor

If you prefer to open and edit multiple files side-by-side, don't fret. This is possible, although it's not immediately obvious how to do it.

To edit files side-by-side, click the Show The Assistant Editor button located in the top right side of the Xcode window (see Figure 15). Once the second editor instance is visible, you can drag files from the project hierarchy on the left into the new editor window. This is particularly helpful when you want to view separate HTML and JavaScript files at the same time, or when you want to view a native plugin and its JavaScript interface at the same time.

Figure 15. Editing code-by-side in Xcode.
Figure 15. Editing code-by-side in Xcode.

Where to go from here

Now that you have Xcode set up with PhoneGap, you are ready to start building some serious applications for iOS using PhoneGap. Next, you may want to read Extending PhoneGap with native plugins for iOS.

Remember, PhoneGap applications are built using HTML, CSS, and JavaScript for the user interface. This enables you to create great looking applications with ease using traditional web development skills. To learn more about rich experiences created with HTML, CSS, and JavaScript, check out the Adobe Developer Connection HTML5 and CSS3 developer center.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Introducing the HTML5 storage APIs
  • Introducing theexpressiveweb.com beta
  • Adobe, standards, and HTML5
  • Developing HTML5 games with Impact JavaScript game engine and Dreamweaver CS5.5
  • Using the Geolocation API
  • CSS3 basics
  • Real-time data exchange in HTML5 with WebSockets
  • Pseudo-classical object-oriented programming in JavaScript with Minion
  • Object types in JavaScript
  • Backbone.js Wine Cellar tutorial – Part 1: Getting started

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement