Access contacts on Android using Phonegap

10,258

Solution 1

Are you waiting for the deviceready event (PhoneGap loaded)?

The following code works for me to put all contacts with a name field into a names array:

function onDeviceReady() {
    // specify contact search criteria
    var options = new ContactFindOptions();
    options.filter="";          // empty search string returns all contacts
    options.multiple=true;      // return multiple results
    filter = ["displayName"];   // return contact.displayName field

    // find contacts
    navigator.contacts.find(filter, onSuccess, onError, options);
}

var names = [];

// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) {
        if (contacts[i].displayName) {  // many contacts don't have displayName
            names.push(contacts[i].displayName);
        }
    }
    alert('contacts loaded');
}

Solution 2

You are working off an old example:

navigator.service.contacts.find(fields, callback, onError, options);

hasn't been the right way to call contacts for quite a few releases. Use:

navigator.contacts.find(fields, callback, onError, options);

instead.

Share:
10,258
Sana Joseph
Author by

Sana Joseph

Updated on June 13, 2022

Comments

  • Sana Joseph
    Sana Joseph almost 2 years

    I'm working on an application using phone-gap. I'm trying to access the contacts on the mobile coz I'll use them later. I'm now trying to write a code to find contacts on the mobile. Here's the JS file I'm using:

    alert('Starting JS');
    var TAP = ('ontouchend' in window) ? 'touchend' : 'click';
    alert('I entered the function'); 
    
       document.addEventListener('DOMContentLoaded', function () {
       alert('I entered the second function');
    
           x$('#friendSubmit').on(TAP, function () {
               var filter = x$('#friendName')[0].value;
               alert('I entered the third function');
    
               if (!filter)
                {
                alert('Cant find contacts');
    
                    // no contents
                    return;
                } 
               else 
               {
                  findContactByName(filter, function (contacts) 
                  {
    
       alert(contacts.length + ' contact(s) found matching "' +filter + '"');
      }
    ); }               
    
      }); });
      function findContactByName(name, callback) {
           function onError() {
               alert('Error: unable to read contacts');
           };
           var fields = ["displayName", "name"],
               options = new ContactFindOptions();
           options.filter = name;
           options.multiple = true;
           // find contacts
           navigator.service.contacts.find(fields, callback, onError,
           options);
         }
    

    None of the alerts are alerted, so it seems that something is wrong in the code (but it alerted when I removed the "findContactByName" function.

    Do you know If I should add any kind of plugins, or update anything so that these functions can work ? I'm working with cordova version 1.6.1 and I updated the permissions at the manifest to be able to access the contacts. So, do you know what's wrong with my code & why isn't it working?

    Thanks a lot.

  • Mit Bhatt
    Mit Bhatt over 10 years
    Thanks man... This is for latest Versions of Phone gap +10 :)
  • dikkini
    dikkini almost 10 years
    Is this way for iPhone too?