Introduction to Apache Cordova
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.
Figure 1.1 Apache Cordova Application Packaging Process
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.
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.
Figure 1.2 Apache Cordova Native Application Architecture Post-3.0
Cordova currently provides the following APIs:
Each of these APIs is described in detail in Chapters 2 through 16. At least one complete sample application is provided for each.