Sana Joseph
Sana Joseph

Reputation: 1948

How can I access a contact's phone number in a PhoneGap running on Android

I am attempting to access a contact's phone number in PhoneGap "Cordova 1.6.1" and send messages to this number. I have been testing the application on the Android mobile platform and I am able to access the name of a contact using this function:

    $('#contacts').append('<option value="'+contacts[i].name+'" >' + contacts[i].name.formatted + '</select>');

However, when I enter:

     contacts[i].phoneNumbers.formatted 

The output is Undefined.

What is the issue I am running into? How may I properly access a contact's phone number in a PhoneGap android application.

Upvotes: 1

Views: 6552

Answers (3)

dbaq
dbaq

Reputation: 1357

You can also use https://github.com/dbaq/cordova-plugin-contacts-phone-numbers to retrieve only the contacts with at least one phone number.

Upvotes: 0

AFIF KALLEL
AFIF KALLEL

Reputation: 41

check out if u'r phoneNumbers array is not null

something like this

if (contacts[i].phoneNumbers){
for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
     alert(     " "  + contacts[i].phoneNumbers[j].value + "\n" + 
           );
}

}

Upvotes: 4

egbrad
egbrad

Reputation: 2417

phoneNumbers is an array, so you need to index into it.

for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
     alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" + 
           "Value: "  + contacts[i].phoneNumbers[j].value + "\n" + 
           "Preferred: "  + contacts[i].phoneNumbers[j].pref);
}

http://docs.phonegap.com/en/1.6.1/cordova_contacts_contacts.md.html#ContactField

Upvotes: 5

Related Questions