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.
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: