concept JavaScript in category cordova

appears as: JavaScript, JavaScript
Apache Cordova in Action

This is an excerpt from Manning's book Apache Cordova in Action.

This is where Cordova enters the picture. Cordova is an open source framework that lets you convert HTML, JavaScript, and Cascading Style Sheets (CSS) into a native application that can run on iOS, Android, and other mobile platforms. Cordova uses a native “wrapper” around a web view (think of it as an embedded browser), commonly called a hybrid mobile application. It also provides access to hardware features like the camera and accelerometer. Unlike a simple web page, Cordova applications can be found (and sold!) in app stores just like native-developed applications. Figure 1.1 gives you a visual demonstration of how hybrid mobile applications work. Let’s look at how Cordova came about and what it gives developers.

Figure 1.1. A simple diagram of how a hybrid app works

As a concrete example, imagine your web page wants to let a user select a friend from the contact list to receive a free kitten. (Kittens!) Right now, HTML pages running in a mobile browser have no access to contacts on the device, but a native app does. Cordova would let you write JavaScript to work with the user’s contacts and do amazing things (with kittens) in a native application.

Other options exist for building mobile applications without native code. Titanium by Appcelerator (www.appcelerator.com/titanium/) is one such example. While it lets you build mobile applications, it doesn’t rely on web standards and primarily uses JavaScript. This allows the tool to build straight to native code and skip using a web view like Cordova, but it may not be as friendly, or familiar, to web developers.

So far you’ve concentrated on using the Cordova CLI to translate web stuff (HTML, JavaScript, and CSS) to native code. You’ve seen how to create a Cordova project and how to test it as a native application on either an emulator or a real device. The last piece of the puzzle is tapping into the rich set of device features that websites do not have access to: the filesystem, the camera, and so on. This was mentioned in chapter 3 as a problem with your simple application. This is where plugins enter the picture.

Suppose you want to write a Cordova application to access your phone’s camera and take a picture. JavaScript, by itself, doesn’t have access to the camera. One of the great features of Cordova is that it provides a mechanism to allow JavaScript to access things it normally wouldn’t be able to. This is done via plugins.

Figure 4.1. Cordova plugins stand between and facilitate communication between JavaScript and the device.