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
About the Authors
About the Reviewers
Related Plugin Resources

Time for action – adding a new contact

We will directly go to the example of creating a new contact entry using the Contacts API. The code for this is as follows:

<script type=”text/javascript” charset=”utf-8”>
    // Binding to the events
    document.addEventListener(“deviceready”, onDeviceReady, false);

    // device APIs are ready and available to use
    function onDeviceReady() {
        var myContact = navigator.contacts.create({“displayName”: “Purus Test”});

        var phoneNumbers = [];

        phoneNumbers[0] = new ContactField(‘work', ‘1234567890', false);
        phoneNumbers[1] = new ContactField(‘mobile', ‘2345678901', true); // preferred
        phoneNumbers[2] = new ContactField(‘home', ‘3456789012', false);

        // You can add any other values of Contact object with name and phone numbers
        myContact.phoneNumbers = phoneNumbers;,onSaveContactError);


    function onSaveContactSuccess(contact) {