Apache Cordova API Cookbook, 1st edition

  • John M Wargo


Using Apache Cordova, mobile developers can write cross-platform mobile apps using standard HTML5, JavaScript, and CSS, and then deploy those apps to every leading mobile platform with little or no re-coding. Apache Cordova API Cookbook provides experienced mobile developers with details about how each Cordova API works and how to use the APIs in their own applications. Coverage is included for all Cordova/PhoneGap 3 APIs.


Instead of just showing short snippets of code to explain a particular API, this guide is chock full of complete examples. You’ll find more than thirty complete Cordova applications that work on Android, iOS, Windows, and more. The sample applications demonstrate exactly what each API does and how it works, while the chapter content describes the limitations on the leading target platforms (and even offers possible workarounds).


Through realistic “cookbook” example code, mobile developer John Wargo helps you master the Cordova APIs and understand how to use them in your Cordova applications.


Topics include

  • Accelerometers, compass, and geolocation
  • Image, video, and audio–capture, playback, and management
  • Determining connection and device information
  • Interacting with the Contacts application
  • Responding to application events
  • Accessing the device file system
  • Globalizing apps
  • Using the InAppBrowser
  • Notifications
  • Custom splash screens 

Special care has been taken to make the code easily readable and digestible by the reader. This guide provides the most accessible coverage, anywhere, of Apache Cordova APIs.



Table of contents

Foreword xiii

Preface xv

Acknowledgments xvii

About the Author xix


Chapter 1: Introduction to Apache Cordova 1

Introduction to Apache Cordova 1

Supported Platforms 5

Coding Cordova Applications 5

Configuring a Cordova Development Environment 6

Building Cordova Applications 6

Anatomy of a Cordova Application 9

Resources 15

Wrap Up 17


Chapter 2: Accelerometer 19

Measuring Device Orientation 20

Watching a Device’s Orientation 28

What Can Go Wrong 33


Chapter 3: Camera 35

Taking a Picture 36

Configuring Camera Options 44

iOS-Only Capabilities 58

What Can Go Wrong 58


Chapter 4: Capture 61

Using the Capture API 62

Configuring Capture Options 66

Capture in Action 66

What Can Go Wrong 77


Chapter 5: Compass 79

Getting the Device Heading 79

Watching the Device Heading 86

What Can Go Wrong 95


Chapter 6: Connection 97

Using the Connection API 97

An Example 99

What Can Go Wrong 103


Chapter 7: Contacts 105

Creating a Contact 106

Searching for Contacts 115

Cloning Contacts 124

Removing Contacts 124

What Can Go Wrong 124


Chapter 8: Device 127

Using the Device API 127

An Example 128

What Can Go Wrong 133

Chapter 9: Events 135

Managing Event Listeners 136

deviceready Event 137

Application Status Events 138

Battery Status Events 141

Button Events 144

Network Status Events 149

What Can Go Wrong 152


Chapter 10: File 153

Storage Locations 154

Accessing the Device’s File System 156

Reading Directory Entries 159

Accessing FileEntry and DirectoryEntry

Properties 163

Writing Files 167

Reading Files 170

Deleting Files or Directories 172

Copying Files or Directories 173

Moving Files or Directories 173

Transferring Files 174

What Can Go Wrong 179


Chapter 11: Geolocation 181

Getting a Device’s Current Location 182

Watching a Device’s Location 189

What Can Go Wrong 197


Chapter 12: Globalization 201

Example Applications 202

Using the Globalization API 204

Currency 206

Date 208

Language and Locale 217

Number 218

What Can Go Wrong 223


Chapter 13: InAppBrowser 225

Example Application 225

Managing an InAppBrowser Window 227

InAppBrowser Events 236

Executing Scripts in an InAppBrowser Window 237

Insert CSS 238

What Can Go Wrong 239


Chapter 14: Media 241

The Media Object 241

Playing Audio Files 247

Media Playback in Action 249

Recording Audio Files 259

Media Recording in Action 259

What Can Go Wrong 263


Chapter 15: Notification 265

Visual Alerts 266

Getting Input 269

Audible and Tactile Notifications 275

What Can Go Wrong 275


Chapter 16: Splashscreen 277

Using the Splashscreen API 277

Using Custom Splash Screen Images 281

What Can Go Wrong 284


Index 285

Published by Addison-Wesley Professional (July 9th 2014) - Copyright © 2015