Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access contacts on Android using Phonegap

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.

like image 795
Sana Joseph Avatar asked Dec 28 '22 02:12

Sana Joseph


2 Answers

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');
}
like image 119
Paul Beusterien Avatar answered Dec 29 '22 15:12

Paul Beusterien


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.

like image 29
Simon MacDonald Avatar answered Dec 29 '22 16:12

Simon MacDonald