Chapter 4. Using plugins to access device features


This chapter covers

  • What plugins are and what they provide for Cordova projects
  • How to find and evaluate plugins
  • How to use the CLI to manage plugins
  • How to use the deviceready event

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.

4.1. What are plugins?

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.

Plugins work by providing a way for your JavaScript to communicate to the device. Figure 4.1 illustrates this.

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

4.2. Finding (and evaluating) plugins

4.3. Managing plugins and the Cordova CLI

4.4. Plugins and the development cycle

4.5. The deviceready event

4.6. Plugin example: Dialogs

4.7. Plugin example: Camera

4.8. Plugin example: Contacts

4.9. Summary