Book Image

PhoneGap 3.x Mobile Application Development HOTSHOT

By : Kerri Shotts
Book Image

PhoneGap 3.x Mobile Application Development HOTSHOT

By: Kerri Shotts

Overview of this book

<p>PhoneGap allows you to use your existing knowledge of HTML, CSS, and JavaScript to create useful and exciting mobile applications.<br /><br />This book will present you with 12 exciting projects that will introduce you to the dynamic world of app development in PhoneGap. Starting with their design and following through to their completion, you will develop real-world mobile applications. Each app uses a combination of core PhoneGap technologies, plugins, and various frameworks covering the necessary concepts you can use to create many more great apps for mobile devices.</p>
Table of Contents (21 chapters)
PhoneGap 3.x Mobile Application Development HOTSHOT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Mobile applications can be developed using various approaches, programming languages and frameworks. Most apps are developed using native tools, that is, they are written in programming languages such as Java or Objective-C and use native software development kits (SDKs). This usually offers the greatest flexibility and performance, but at a cost. What happens when your app needs to work across many different platforms? Often, this involves rewriting a larger portion of the app from scratch. Once another platform is added to the mix, the entire process is repeated. Given that most apps are written by small teams, or even a single individual, this quickly becomes unsustainable. There's got to be a better way!

All current mobile platforms support web apps. These applications are coded entirely in HTML, Cascading Style Sheets (CSS), and JavaScript. There may also be a server-side component, depending on the app's purpose. As long as the app doesn't need to interact with the device's capabilities at a deeper level than that provided by the browser, this is fine. But the moment the app needs to access these capabilities (such as the physical file system), the browser environment quickly becomes too restrictive.

Cordova aims to bridge the gap between web apps and native device functionality. You can write HTML, CSS, and JavaScript, and Cordova will provide a native shell in which that code executes. This native shell is different for each platform, but it provides core device functionality in a consistent way that simplifies cross-platform development.

PhoneGap is Adobe's extension to Cordova. PhoneGap provides a slightly different interface, and it provides a remote building service that reduces the need to have a complete software development stack installed on your development machine. You are free to use either PhoneGap or Cordova in this book; we will focus on the latter and try, where possible, to point out the differences if there are any.

Because Cordova and PhoneGap wrap your HTML, CSS, and JavaScript code into a native shell, you can also submit your app to various app stores (most app stores don't accept web app submissions). If you do submit your app, you need to keep in mind that app stores often have human interface guidelines and are often very strict about how your app should behave. When you submit an app to an app store, it should reflect and respect the conventions of that platform. Furthermore, many app stores will refuse an app that is only a wrapper for a remote website. Instead, your app should consist of client-side HTML, CSS, and JavaScript code. Should your app need to communicate with a server, it can do so via XMLHttpRequest (XHR).

While many books that cover Cordova and PhoneGap will cover the framework and its documentation, this book is different. We'll introduce you to full applications. Each one is designed to take advantage of one or more native device features but is also fully functional, interesting, and useful.

This book focuses on the iOS and Android platforms and shows you how to write cross-platform code that works on both platforms in a single code base. Even so, it should be relatively easy to get these apps up and working on any other platform supported by Cordova and PhoneGap, such as BlackBerry OS, Windows Phones, and so on, with minor modifications.

What this book covers

Project 1, Your First Project, introduces you to project creation and management via the Cordova/PhoneGap command-line interfaces. We'll create a simple project and execute it on a simulator or device.

Project 2, Localization and Globalization, deals with creating a template that can be used for this and future projects using Cordova's command-line interface. We'll cover localization and globalization across four locales: English (UK and US) and Spanish (Spain and US). We'll be introduced to jQuery/Globalize and the mechanisms behind number and currency formatting as well as a translation matrix implementation.

Project 3, Mobile Application Design, the first version of a note-taking app called Filer. We'll be introduced to RequireJS (dependency management) and Yet Another Simple Mobile Framework (YASMF). We'll be designing the user interface and user interactions and will implement the required data models and views. We'll cover using LocalStorage and why it isn't a great idea for persistent data storage.

Project 4, The File API, explores persistent data storage, which is required by most apps, and Filer is no exception. We'll create Version 2 of our Filer app by modifying Filer to use the device's physical filesystem. We'll be introduced to the HTML5 File API and the core plugin Cordova uses to provide this functionality.

Project 5, Working with Audio, explains that notes don't need to be text-only; in this project, we'll create the third version of Filer by adding support for audio notes. We'll be introduced to the Media API and the corresponding plugin. We'll add a swipe-to-delete gesture using Hammer.js, and we'll cover the view stack, the view lifecycle, and view-to-view animations.

Project 6, Working with Still Images, deals with extending Filer to allow the user to take a picture using the device's camera and attach it to a note. We'll be introduced to the camera API and associated plugin, and we'll also discuss how to display many image thumbnails at once in a memory-efficient manner.

Project 7, Capturing Video, discusses rounding out the media capabilities of the app by adding video support in version four of Filer. We'll be introduced to the HTML5 Video tag, and we'll work with the Media Capture API and plugin.

Project 8, Sharing Content, covers adding shared capabilities to Filer. We'll be introduced to a third-party plugin that provides access to e-mail and various social networks. We'll also cover how to share text and images.

Project 9, Devices of Different Sizes, pursues two different mechanisms for filling the screens of larger, tablet-sized devices since we have only focused on phone-sized devices so far. We'll be introduced to split view controllers as well as various mechanisms for building apps that respond to screen size.

Project 10, Maps and GPS, deals with building a totally new app that records the location of a user over a given period of time. We'll use the Geolocation API and plugin to access the user's location, and we'll use Google Maps to display the recorded route to the user.

Project 11, Canvas Games and the Accelerometer, focuses on creating a simple game called Cave Runner that uses the HTML5 canvas and uses the device's accelerometer as one of the input mechanisms.

Project 12, Building a Backend – Working with Parse, extends Cave Runner by adding a simple backend to keep track of high scores. We'll be introduced to Parse and their JavaScript API used to store and retrieve data.

The online project, Using Native Controls, will take the app from Project 10, Maps and GPS, and add various native controls to make it look and feel even more native.There are times when we need our app to blend into the native environment in a better way than what we can provide via HTML, CSS, and JavaScript. While this project is tailored for iOS, the concepts can be extended to other platforms. This project is only available online at https://www.packtpub.com/sites/default/files/downloads/7925OS_Chapter_13_Native_Controls.pdf.

Appendix A, User Interface Resources, covers various user interface and design patterns that are used in mobile applications.

Appendix B, Tips, Tricks, and Quirks, expands on the idea that each platform has its quirks, and even though we're writing cross-platform code, we will sometimes encounter glitches. In this appendix, we'll cover some of the quirks we encountered while writing the apps in this book as well as cover some tips and tricks learned from experience.

What you need for this book

To build/run the code supplied for the book, the following software is required (divided by platform, where appropriate):

 

Windows

Linux

OS X

For iOS Apps

   

IDE

  

XCode 5+

OS

  

OS X 10.8.4+

SDK

  

iOS 6+

For Android Apps

   

IDE

Eclipse 3.6.2+ or Android Studio Preview

OS

XP or newer

Any modern distribution supporting: GNU C library 2.7+ (if 64-bit, must be able to run 32-bit apps); Ubuntu must be 8.04+

OS X 10.5.8+

Java*

JDK 6 or higher

JDK 6 or higher

JDK 6 or higher

SDK

Version 15+

Version 15+

Version 15+

For all platforms

   

Apache Cordova / PhoneGap

3.x**

3.x**

3.x**

ANT

1.8+

1.8+

1.8+

* A JRE is not sufficient.

** The code accompanying this book has been tested with Cordova 3.4. It should work with any 3.x version. The code also relies on core and third-party plugins; check the README file in the code package for this book for specific plugin versions, if applicable.

While not required, it is considered good practice to work using source control. Git is an easy-to-use source control management solution, and the one I use.

Websites and download locations that can be useful are as follows:

Who this book is for

If you are a developer who has experience with HTML, CSS, and JavaScript and desires to use your experience to build mobile applications, this book is for you. You should understand HTML, be able to understand CSS, and have a reasonable working knowledge of JavaScript. You should also be able to set up a development environment and should be comfortable with a text editor or integrated development environment.

This book is also for any native developer who wants to create apps that can target multiple platforms with limited modifications. Cordova and PhoneGap provide mechanisms that make it easy to build a single HTML/CSS/JavaScript codebase that works across many platforms.

Conventions

In this book, you will find several headings appearing frequently. To give clear instructions of how to complete a procedure or task, we use:

What do we build?

This section explains what you will build.

What does it do?

This section explains what the project will achieve.

Why is it great?

This section explains why the project is cool, unique, exciting, and interesting. It describes what advantage the project will give you.

How are we going to do it?

This section explains the major tasks required to complete your project.

  • Task 1

  • Task 2

  • Task 3

  • Task 4

What do I need to get started?

This section explains any prerequisites for the project, such as resources or libraries that need to be downloaded, and so on.

Task 1

This section explains the task that you will perform.

Getting ready

This section explains any preliminary work that you may need to do before beginning work on the task.

Getting on with it

This section lists the steps required in order to complete the task.

What did we do?

This section explains how the steps performed in the previous section allow us to complete the task.

What else do I need to know?

The extra information in this section is relevant to the task.

In this book, you also will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, and pathnames are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

var userLocale = navigator.language || navigator.browserLanguage || 
navigator.systemLanguage || navigator.userLanguage;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

             var theUID = this.getAttribute("data-uid");
             var theUID = this.getAttribute("data-uid");

Any command-line input or output is written as follows:

cordova create ./HelloWorld helloworld.phonegaphotshot.com HelloWorld

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Click on the Downloads tab, and find the Command Line Tools option."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

If GitHub is more your style, you can also download the code for this book from https://github.com/kerrishotts/PhoneGap-HotShot-3-x-Code-Bundle.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.