Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
PhoneGap Beginners Guide (third edition)
PhoneGap Beginners Guide (third edition)

PhoneGap Beginners Guide (third edition): Create, develop, debug, and deploy your very own mobile applications with PhoneGap

Arrow left icon
Profile Icon Ramanujam Profile Icon Giorgio Natili
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (6 Ratings)
Paperback Jul 2015 284 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Ramanujam Profile Icon Giorgio Natili
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (6 Ratings)
Paperback Jul 2015 284 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

PhoneGap Beginners Guide (third edition)

Chapter 2. Building Your First PhoneGap Project

In Chapter 1, Introduction to PhoneGap, you learned about one of the problems PhoneGap is trying to solve—developing mobile applications consistently across multiple platforms—and how to set up your build environment. Next, you will delve into the internals of PhoneGap. First, you need to know how the project is structured and how the CLI tools are used.

In this chapter, you will:

  • Take a look at the structure of a PhoneGap/Cordova application
  • Learn about using the command-line tools of Cordova
  • Know about Cordova lifecycle events
  • Create a sample example
  • Get an overview of the browser's debugging tools
  • Review the debug workflow with mobile in mind
  • Learn how to build and deploy the applications

The structure of a PhoneGap application

After creating a new project, as explained in the previous chapter, you'll see the following structure inside the project's root folder:

  • www: This directory contains the source files of the application and so we will be playing a lot in this directory. It contains the css, js, and img subdirectories, where you can place the respective files. Apart from assets such as CSS, JavaScript, and images, we will also have application-related HTML files.
  • merges: In this folder, you can add any platform-specific customization so you don't have to modify the source files every time you work with a project. For example, we can use this feature to use a different font style on Android devices only. The platforms that you want to override will have a specific folder similar to the www folder having its own CSS, JS, HTML, and image contents:
    merges/
    |-- ios/
    | '-- app.js
    |-- android/
    | '-- app.js
    www/
    '-- app.js

    In the preceding directory...

Using the PhoneGap CLI

You can use the Cordova command-line interface to initialize the project code, after which you can use various platforms' SDKs to develop them further. In the previous chapter, we discussed how to create a new project, add the required platforms, and build them using the CLI.

Apart from creating a project by using the CLI tool, there are several other functions carried out by the CLI of Cordova. As the steps for installing the CLI are already covered, let's move on with advanced CLI usage. Once you create a project, use cd to move into it and you can execute a variety of project-level commands.

The following is the list of the most used CLI commands:

  • platform add <platform>: This adds a platform as a build target for the project.
  • platform [rm | remove] <platform>: This removes a platform which was previously added to the project.
  • platform [ls | list]: This lists all platforms for which the project will build.
  • platform [up | update] <platform&gt...

Cordova events

To maximize the benefits of using Cordova, you should know about all the events available. They are called lifecycle events because they are a part of your application throughout its lifecycle. These events are available by default for all applications and it's up to the developer to use them to implement better design. Although there are several events, we will discuss the most important and commonly used events.

The deviceready event

The deviceready event is an important event of Cordova and you can't live without it in the Cordova world. This event is triggered when Cordova has fully loaded and the application is ready to be used. We should know when the application is ready to be used and so this event comes to our rescue. This event should be the gateway to all the application's functionality:

document.addEventListener("deviceready", function() {
    // Application starts here
});

To make the code easy to understand, we can define the function...

Time for action – the Hello World example

PhoneGap is an intermediary layer that talks to the mobile device and the application; the app resides inside a browser, and, using the PhoneGap API, you can connect to phone features such as contacts and camera.

The UI layer of a PhoneGap application is a web browser view that takes up 100 percent of the device's width and height; think of the UI layer as a browser. The UI layer is known as WebView. The WebView used by PhoneGap is the same one used by the native operating system.

Having discussed the basics of PhoneGap and the command-line tools, we will now create a simple application. This is not the typical Hello World example. With the already learned commands and configured environment with npm, let's create a new project:

C:\> phonegap create example1
C:\> cd example1
C:\example> phonegap platform add android

With the completion of the preceding commands, we created a new project called example1 and added Android platform...

WebKit debugging – Chrome, Safari, and Opera

WebKit-based browsers support various debugging tools. For example, when encountering JavaScript issues, you can launch Web Inspector or Developer Tools and start to explore logs and errors using the JavaScript console.

In Chrome, you can access the Developer Tools from the Customize menu (click on the Customize menu and then go to Tools | Developer Tools). The Customize menu is available in the top-right corner. When working with Safari, you first have to enable the Developer Tools by opening Safari's Preferences panel and then selecting the Show Develop menu in the menu bar checkbox. You can then access the inspector by choosing Show Web Inspector from the application's Develop menu.

Since the Web Inspector is part of the WebKit codebase, you can use the same shortcuts in Chrome and Safari to access the debugging tools.

On Windows and Linux, press:

  • Ctrl + Shift + I to open Developer Tools
  • Ctrl + Shift + J to open Developer Tools...

Gecko debugging – Firefox

Firefox is based on the Gecko open source layout engine used in many applications developed by the Mozilla Foundation and the Mozilla Corporation. It offers good debugging tools and it's evolving quickly, including innovative projects such as Desktop WebRT, which lets you build a desktop web application at runtime that provides web apps with a native-like look and feel along with platform integration APIs on Windows, OS X, and other desktop platforms.

If you are not developing apps for Android or iOS, you can use the Firefox layout engine, which offers some powerful development and debugging tools. Let's quickly explore how to use Firefox/Firebug to inspect and debug your app; as you will see, there are several similarities between the debug tools available in WebKit and Firefox.

Firebug integrated with Firefox puts a great set of developer tools at your fingertips that rivals the features of the WebKit Web Inspector. In order to install the Firebug...

Internet Explorer

Internet Explorer, at the time of writing, still has a wide install base; it's also the least favorite browser among developers. Virtually every developer has experienced serious issues when optimizing a webpage for IE; this is due to the fact that IE diverges from web standards in significant areas, but things are changing and the preview of IE 10 is getting good scores in various tests. Internet Explorer 11 is even better for developers.

Developer Tools were introduced in Internet Explorer 8, and updated with new functionality in Internet Explorer 9. Developer Tools in Internet Explorer 10 add Web Worker debugging and support for multiple script sources.

You can access Developer Tools by pressing F12 or by navigating to Tools | Developer Tools from the menu bar:

Internet Explorer

The IE 10 Developer Tools provide a similar user interface to the Developer Tools in Safari, Chrome, and Firefox.

The structure of a PhoneGap application


After creating a new project, as explained in the previous chapter, you'll see the following structure inside the project's root folder:

  • www: This directory contains the source files of the application and so we will be playing a lot in this directory. It contains the css, js, and img subdirectories, where you can place the respective files. Apart from assets such as CSS, JavaScript, and images, we will also have application-related HTML files.

  • merges: In this folder, you can add any platform-specific customization so you don't have to modify the source files every time you work with a project. For example, we can use this feature to use a different font style on Android devices only. The platforms that you want to override will have a specific folder similar to the www folder having its own CSS, JS, HTML, and image contents:

    merges/
    |-- ios/
    | '-- app.js
    |-- android/
    | '-- app.js
    www/
    '-- app.js

    In the preceding directory structure, the global application...

Using the PhoneGap CLI


You can use the Cordova command-line interface to initialize the project code, after which you can use various platforms' SDKs to develop them further. In the previous chapter, we discussed how to create a new project, add the required platforms, and build them using the CLI.

Apart from creating a project by using the CLI tool, there are several other functions carried out by the CLI of Cordova. As the steps for installing the CLI are already covered, let's move on with advanced CLI usage. Once you create a project, use cd to move into it and you can execute a variety of project-level commands.

The following is the list of the most used CLI commands:

  • platform add <platform>: This adds a platform as a build target for the project.

  • platform [rm | remove] <platform>: This removes a platform which was previously added to the project.

  • platform [ls | list]: This lists all platforms for which the project will build.

  • platform [up | update] <platform>: This...

Cordova events


To maximize the benefits of using Cordova, you should know about all the events available. They are called lifecycle events because they are a part of your application throughout its lifecycle. These events are available by default for all applications and it's up to the developer to use them to implement better design. Although there are several events, we will discuss the most important and commonly used events.

The deviceready event

The deviceready event is an important event of Cordova and you can't live without it in the Cordova world. This event is triggered when Cordova has fully loaded and the application is ready to be used. We should know when the application is ready to be used and so this event comes to our rescue. This event should be the gateway to all the application's functionality:

document.addEventListener("deviceready", function() {
    // Application starts here
});

To make the code easy to understand, we can define the function separately and bind it to...

Left arrow icon Right arrow icon

Description

This book is for web developers who want to be productive in the mobile market quickly. In fact, by using PhoneGap, it's possible to deploy native applications based on web standards. This book assumes a very small knowledge of HTML/CSS/JavaScript and mobile platforms, such as Android, BlackBerry, iOS, and Windows Phone, and takes the reader step-by-step into a deep overview of PhoneGap and its APIs.

What you will learn

  • Get acquainted with the fundamentals of PhoneGap
  • Use HTML, CSS, and JavaScript to create, debug, and deploy your own mobile applications
  • Work with mobilespecific frameworks such as iOS and Android to design your applications
  • Install and work with all PhoneGap/Cordova plugins
  • Implement deviceoriented APIs related to contacts, files, the camera, and geolocations
  • Use PhoneGap/Cordova events for accessibility
  • Enhance and optimize your application for realtime environments

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 28, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781784392284
Vendor :
Apache
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 28, 2015
Length: 284 pages
Edition : 1st
Language : English
ISBN-13 : 9781784392284
Vendor :
Apache
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 146.97
Learning Ionic
$48.99
PhoneGap Beginners Guide (third edition)
$48.99
PhoneGap By Example
$48.99
Total $ 146.97 Stars icon
Banner background image

Table of Contents

15 Chapters
1. Introduction to PhoneGap Chevron down icon Chevron up icon
2. Building Your First PhoneGap Project Chevron down icon Chevron up icon
3. Mobile Frameworks Chevron down icon Chevron up icon
4. Working with Plugins Chevron down icon Chevron up icon
5. Using Device Storage and the Files API Chevron down icon Chevron up icon
6. Using the Contacts and Camera APIs Chevron down icon Chevron up icon
7. Accessing the Device Sensors and Locations API Chevron down icon Chevron up icon
8. Advanced PhoneGap Chevron down icon Chevron up icon
9. Getting Ready for Release Chevron down icon Chevron up icon
10. A Sample PhoneGap Project Chevron down icon Chevron up icon
A. The JavaScript Quick Cheat Sheet Chevron down icon Chevron up icon
B. Publishing Your App Chevron down icon Chevron up icon
C. Related Plugin Resources Chevron down icon Chevron up icon
D. PhoneGap Tools Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(6 Ratings)
5 star 33.3%
4 star 33.3%
3 star 16.7%
2 star 0%
1 star 16.7%
Filter icon Filter
Top Reviews

Filter reviews by




Don Aug 19, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book was insightful and had an approachable layout and all the content necessary to get up and running with PhoneGap. As I expect from an IT book, the content of this publication contained a step-by-step depiction of installing, using, and testing an application. What's more, I was happy that cross-platform tools, plug-ins, debugging approaches, a nice hands-on example, and a great cheatsheet was provided at the end. If you are serious about developing a multiplatform application, this is a wonderful resource to get you started.
Amazon Verified review Amazon
WebSteve Sep 14, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Do you already know HTML, CSS, and JavaScript? Are you interested in making mobile apps with those skills? This get this book and learn how to parlay your skills into a cross-platform app for Apple, Android, and Windows devices. This book introduces PhoneGap to novices who already know how to make websites and don't want to learn whole new languages to make an app. PhoneGap 3rd Ed. will show you how to integrate the mobile phone's capabilities into your app, such as contacts, camera, accelerometer, compass, globalization, and so on. This book presents the information the way I like it presented - which is tightly focused on integrating one by one. In other words, (The other way is to create a full app and integrate these features into the app, then I have difficulty telling which code is for the app and which is for the feature.) The book gives an overview of mobile frameworks, that is, software tools that will adapt your app to work in devices of various sizes - code once and play (almost) and Apple, Android, and Windows device. It's a great book to start with if you are a beginner who already has web skills. PhoneGap is your ticket to taking those skills to the mobile level.
Amazon Verified review Amazon
T.M. Aug 23, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is a great introduction to PhoneGap, a cross-platform Javascript SDK for building mobile apps. The framework is extensible with plug-ins. There’s extensive coverage of the following APIs: storage, contacts, camera, sensors and location. Localisation and internationalisation are also covered. A chapter details how to build your application, and publishing to various app stores is covered in the appendix.
Amazon Verified review Amazon
Jakub Vosahlo Aug 22, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
In my eyes the book comprises of two parts. The first, major one, gives a quick and sometimes a bit shallow introduction into basic topics like principles, installation, etc. Then it dives into more important topics, like discussion of mobile frameworks, which can be used today with PhoneGap, gives introduction to plugins and in a bit more detail discusses crucial APIs for accessing device files system, camera, contacts, etc. After the brief discussion on finalising the app and building it for release, comes a second part of the book, which walks you through a development of a sample app, using many of the crucial APIs you will be using most often.I like the structure of the book, I take it as a real introductory piece (which was actually great for me, as I never tried PhoneGap/Cordova before) and so it is most suitable for beginners. The writing is clear most of the time, if a bit dry and text-book like, but it is quite easy to follow for a non-native speaker as myself.
Amazon Verified review Amazon
Mark Shackelford Apr 16, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
This is what it says on the cover: "A Beginner's Guide", and is fine as a quick overview of PhoneGap and its possibilities, but I soon found I was reverting to Dr. Google and the various PhoneGap blogs
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.