Reputation: 250
I have written a code following the documentation on phone gap website`
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
}
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
for (var i=0; i<contacts.length; i++)
{
console.log("Display Name = " + contacts[i].displayName);
var element = document.getElementById('cont');
element.innerHTML='<li><a href="#">' + contacts[i].displayName + '</a></li>';
}
}
// onError: Failed to get the contacts
//
function onError(contactError) {
alert('onError!');
}
</script>`
It is working fine in andriod emulator but when i install the application samsung galaxy phone it return a "null". Kindly Help.
Upvotes: 1
Views: 3420
Reputation: 250
It is a complex problem with simple solution. just added only one line of code
options.filter="";
options.multiple=true;
var filter = ["*"];
That is complete code is
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
options.filter="";
options.multiple=true;
var filter = ["*"];
navigator.contacts.find(filter, onSuccess, onError, options);
}
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
var ele = document.getElementById("cont");
var str = '<ul data-role="listview" >';
for (var i=0; i<contacts.length; i++) {
str=str + '<li>' + contacts[i].displayName + '</li>';
}
str = str + '</ul>';
// ele.innerHTML = str;
}
// onError: Failed to get the contacts
//
function onError(contactError) {
alert('onError!');
}
And it is working, by the way it also shows some null values as per the contact entries but it is working fine.
Upvotes: 1
Reputation: 10857
It may be iOS - http://docs.phonegap.com/en/2.1.0/cordova_contacts_contacts.md.html#Contacts. The docs say displayName isn't supported there. I'd follow Littm's advice, and also check the doc I linked to here as it talks about what properties don't work across different platforms.
Upvotes: 0
Reputation: 4947
Strange... I also got null
... (using Phonegap version 2.0)
I suggest you use something like contacts[i].name.formatted
instead of contacts[i].displayName
.
So, for example, by using contacts[i].name.formatted
, your code should look like something like this:
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
}
// onSuccess: Get a snapshot of the current contacts
//
function onSuccess(contacts) {
var element = document.getElementById('cont');
for (var i=0; i<contacts.length; i++)
{
console.log("Display Name = " + contacts[i].name.formatted);
element.innerHTML='<li><a href="#">' + contacts[i].name.formatted + '</a></li>';
}
}
// onError: Failed to get the contacts
//
function onError(contactError) {
alert('onError!');
}
Here are the different information that you can obtain with contacts[i].name
:
contacts[i].name.givenName
: provides the given name
contacts[i].name.formatted
: provides a formatted name (first name + last name)
contacts[i].name.middleName
: provides the middle name
contacts[i].name.familyName
: provides the family name (first name + last name)
contacts[i].name.honorificPrefix
: provides the honorific prefix
contacts[i].name.honorificSuffix
: provides the honorific suffix
Hope this helps.
Upvotes: 2