Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap - only show contacts with phone numbers

Is there a way to only show contacts that has phone number?

Right now i am getting all the contacts and then looping thru each and finding out their phone number but i was wondering if there is a way to pass a parameter to contactFindOptions object so that it only picks up contacts that has phone number?

This is how my code currently looks like:

var options = new ContactFindOptions();
options.filter=""; //Can i pass something here to pick only contacts with phone number
options.multiple=true; 
var fields = ["displayName", "phoneNumbers"];
navigator.contacts.find(fields, onSuccess, onError, options);


function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) {
        console.log("Display Name = " + contacts[i].displayName);
        if(null != contacts[i].phoneNumbers)
            {
                for(var j=0;j<contacts[i].phoneNumbers.length;j++)
                {
                      console.log("Name = " + contacts[i].displayName);
                      console.log("Phone = " + contacts[i].phoneNumber[j].value);

                }
            }
    }
}
like image 613
Asdfg Avatar asked Feb 12 '13 02:02

Asdfg


2 Answers

This plugin looks like the best approach: https://github.com/dbaq/cordova-plugin-contacts-phone-numbers.

It only searches for contacts with phone numbers.

like image 102
Anthony Avatar answered Oct 20 '22 06:10

Anthony


yes, we can use hasPhoneNumber filter option. Code snippet is as follows:

            var contactFindOptions = new ContactFindOptions();
            contactFindOptions.filter = "";
            contactFindOptions.multiple = true;
            contactFindOptions.hasPhoneNumber = true;
            navigator.contacts.find(
                    ["phoneNumbers"],
                    function (contacts) {
                       // you will get contacts in this callback success function
                    },
                    function (e) {
                        if (e.code === ContactError.NOT_SUPPORTED_ERROR) {
                           console.log("Searching for contacts is not supported.");
                        } else {
                            console.log("Search failed: error " + e.code);
                        }
                    },
                    contactFindOptions);

Note: hasPhoneNumber(Android only): Filters the search to only return contacts with a phone number informed. (Boolean) (Default: false)

like image 23
Niru Avatar answered Oct 20 '22 06:10

Niru