Get all contacts using PhoneGap?

phonegap get all contacts
get all contacts using Phonegap
Apache Cordova / Phonegap contact api

Apache Cordova / Phonegap Contact Plugin (https://www.npmjs.com/package/cordova-plugin-contacts) is used for working with phone contact database. Using this plugin, You can create new contact, clone, delete, update contact database.

How to get all contacts using PhoneGap / Apache Cordova?

  • Create New Project for working with contact database
  • Add platforms such as android, iOS or Windows
  • Add Contact plugin that shown below

Install plugin

cordova plugin add cordova-plugin-contacts

Go to your HTML page (www/index.html) and write this code. This code will get all contacts Full name, mobile number and email address in HTML page.
index.html


<!DOCTYPE html>
<html>
 <body>
 <h2>Contacts List</h2>
 <div id="mobile"></div>
 <div id="email"></div>
 <script src="cordova.js"></script>
 <script type="text/javascript">
 document.addEventListener("deviceready", init, false);
function init() {
  navigator.contacts.find([navigator.contacts.fieldType.displayName],gotContacts,errorHandler);
}
function errorHandler(e) {
 console.log("errorHandler: "+e);
}

function gotContacts(c) {
 console.log("gotContacts, number of results "+c.length);

 mobileDiv = document.querySelector("#mobile");
 emailDiv = document.querySelector("#email");

 /* Retriving phoneNumbers */
 for(var i=0, len=c.length; i<len; i++) {
 if(c[i].phoneNumbers && c[i].phoneNumbers.length > 0) {
 mobileDiv.innerHTML += "<p>"+c[i].displayName+"<br/>"+c[i].phoneNumbers[0].value+"</p>";
 }
 }

 /* Retriving Email */
 for(var i=0, len=c.length; i<len; i++) {
 if(c[i].emails && c[i].emails.length > 0) {
 emailDiv.innerHTML += "<p>"+c[i].emails[0].value+"</p>";
 }
 }
}
 </script>
 </body>
</html>

Here navigator.contacts.find() function is used for get / search all contacts

NOTE: It may take time for loading / displaying contacts. It depends upon the number of contacts you have.

  • Ganesh

    hi. i need to send App install invitation using contacts..how to do..

    • use social sharing plugin and sms plugin..!!

  • Nickson Nyakambi

    Hey Sundaravel..this is great. How could you go around posting all the found contacts to a mysql database?

    • Yea!. By using Web service call you can do that

  • Deepak Yadav

    Hi Sundaravel, thank you for the tutorial. How can I insert all the found contacts to a mysql database? Please help me with it!

    • Yes! you can do that, you need to submit your contacts json to your web service

      • Deepak Yadav

        Hi,
        Thank you for responding. Can you please help me with a referral link or a piece of code how to pass the values through your javascript code!

  • selva

    hello sir,
    how to connect database?????? please send the database source code……..

  • harika

    by just adding html page will it display all the contacts that are present in google?

    • No, you need to add plugins

      • harika

        Thanks I got the contact list

        • Always welcome with soft heart 🙂

          • harika

            In this application, we are using javascript.
            Can we do the same by using backbone js?

          • It’s your wish

          • harika

            I’m new to backbone js. Can you explain me how can I display the contacts using backbone in corodva

          • Sorry, I can’t help on this. but you can try my premium service : phonegappro.com/phonegap-training

  • harika

    Hi Sir,
    Can you tell me how can we get the call log by using cordova

  • harika

    Hi sir,
    In the above code from where we will get the value for “variable c”?

    function gotContacts(c) {
    console.log(“gotContacts, number of results “+c.length);

    • c is the output of the contact plugin (it’ll contain JSON object of all contacts)

      • harika

        Thank you sir