This chapter is your introduction to the Apache Cordova framework and Apache Cordova application development. In the chapter, John Wargo describes what Cordova is, how it works, and how to develop applications using Cordova.

This chapter is from the book

This chapter is from the book

It’s clear from many of the support forum posts that developers who are just getting started with Apache Cordova don’t really “get” what they’re working with. This chapter should answer many of the initial questions you have related to Apache Cordova. If you are already familiar with Apache Cordova, you can skip this chapter if you want and jump right away into the Cordova application programming interfaces (APIs).

Introduction to Apache Cordova

Apache Cordova (http://cordova.apache.org/) is a free, open-source framework for building cross-platform native applications using HTML5. The creators of Apache Cordova wanted a simpler way of building cross-platform mobile applications and decided to implement it as a combination of native and web application technologies. This type of mobile application is called a Hybrid application.

The initial benefit of Apache Cordova is the native capabilities above and beyond what is normally supported in the mobile browser. At the time all of this started, the best way to build a mobile application that worked on multiple mobile devices was to build it using HTML. Unfortunately, though, for mobile developers, many mobile applications needed to do more than HTML and web browsers could support. Building a web application that interacted with the device camera or the local Contacts application simply wasn’t possible. To get around this, Cordova implements a suite of APIs that extend native device capabilities (such as the camera, accelerometer, Contacts application, and so on) to a web application running within the native container. The rest of the book beyond this introductory chapter is all about those APIs.

Apache Cordova consists of the following components:

Source code for a native application container for each of the supported mobile device platforms. The container renders the Cordova web application on the device.

A set of Core APIs (delivered as plugins) that provide a web application running within the container access to native device capabilities (and APIs) not normally supported by a mobile web browser.

A set of tools used to manage the process of creating application projects, managing plugin lifecycle, building (using native software development kits—SDKs) native applications, and testing applications on mobile device simulators and emulators.

To build a Cordova application, you create a web application, package the web application into the native container, test and debug the application, and then distribute it to users (typically through an app store). The packaging process is illustrated in Figure 1.1.

When many developers first learn about this technology, they immediately assume that the web application is somehow translated into the native language for each supported mobile device platform—converted into Objective-C for iOS or Java for Android, for example—but that’s not what’s happening here. There are some mobile application frameworks that take that approach, but for Cordova, the web application simply runs unmodified within a native application shell.

Within the native Cordova application, the application’s user interface (UI) consists of a single screen that contains nothing but a single web view that consumes the available screen space on the device. When the application launches, it loads the web application’s start-up page (typically index.html but easily changed by the developer to something else) into the web view, then passes control to the web view to allow the user to interact with the web application. As the user interacts with the application’s content (the web application), links or JavaScript code within the application can load other content from within the resource files packaged with the application or can reach out to the network and pull content down from a web or application server.

ABOUT WEB VIEWS

A web view is a native application component that is used to render web content (typically HTML pages) within a native application window or screen. It’s essentially a programmatically accessible wrapper around the built-in web browser included with the mobile device.

The web application running within the container is just like any other web application that would run within a mobile web browser. It can open other HTML pages (either locally or from a web server sitting somewhere on the network), and JavaScript embedded within the application’s source files implements needed application logic, hiding or unhiding content as needed within a page, playing media files, opening new pages, performing calculations, retrieving content from or sending content to a server. The application’s look-and-feel is determined by any font settings, lines, spacing, coloring, or shading attributes added directly to HTML elements or implemented through Cascading Style Sheets (CSS). Most anything a developer can do in a web application hosted on a server can also be done within a Cordova application.

A typical mobile web browser application does not usually have access to device-side applications, hardware, and native APIs. For example, the Contacts application is not accessible to web applications, nor can a web application typically interact with the accelerometer, camera, microphone, and more or determine the status of the device’s network connection. The typical native mobile application, on the other hand, will make frequent use of those capabilities. An interesting mobile application (interesting to prospective application users anyway) likely needs access to those native device capabilities.

Cordova accommodates that need by providing a suite of JavaScript APIs that a developer can leverage to enable a web application running within the Cordova container to access device capabilities outside of the web context. Essentially these APIs are implemented in two parts: a JavaScript library that exposes the native capabilities to the web application, and the corresponding native code running in the container that implements the native part of the API. This is implemented essentially as one JavaScript library but with separate native implementations on each supported mobile device platform.

Beginning with Cordova 3.0, each of the Cordova APIs has been broken out into separate plugins; you can use the Cordova command-line interface (CLI) or plugin manager (plugman) to add and remove plugins from your Cordova project. This approach provides the architecture illustrated in Figure 1.2, an application with discrete code for each plugin and where only the needed plugins are packaged with the application.