Book Image

PhoneGap Beginners Guide (third edition)

Book Image

PhoneGap Beginners Guide (third edition)

Overview of this book

Table of Contents (22 chapters)
PhoneGap Beginner's Guide Third Edition
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Related Plugin Resources
Index

Time for action – searching device contacts


We will now see an example of getting all the contacts from the device and listing them down:

  1. Create a new project using the PhoneGap CLI:

    $ phonegap create ContactsApi
    
  2. Change the current working directory to the directory that is newly created:

    $ cd ContactsApi
    
  3. Add the required platforms to the project. We will add Android for this example:

    $ phonegap platform add android
    
  4. Install the Contacts plugin to the project:

    $ phonegap plugin add cordova-plugin-contacts
    
  5. In the www/index.html file, you need to replace the content and add the following code. To start with, let's create an event listener and bind that to a function. In the following code, when the device is ready, the OnDeviceReady method will be fired:

    <script type=”text/javascript” charset=”utf-8”>
        document.addEventListener(“deviceready”, onDeviceReady, false);
    </script>
  6. Now, let's add the content for the OnDeviceReady method:

    function onDeviceReady() {
        var options = new...