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 – 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;

        myContact.save(onSaveContactSuccess,onSaveContactError);

    }

    function onSaveContactSuccess(contact) {
     ...