I am trying to access the contacts on a phone. I am using to code below to do so, but the navigator.contacts.find isn't working. It doesn't return an error or a success message. If I put any type of alert after that line of code it will not appear.
function read_contacts(){
var options = new ContactFindOptions( );
options.filter = ""; //leaving this empty will find return all contacts
options.multiple = true; //return multiple results
var filter = ["displayName"]; //an array of fields to pare against the options.filter
navigator.contacts.find(filter, successFunc, errFunc, options); //breaking the code
function successFunc( matches ){
alert("reading contacts...");
for( var i=0; i<matches.length; i++){
alert( matches[i].displayName );
}
function errFunc(){
alert("Error finding contacts");
}
}
}
I am trying to access the contacts on a phone. I am using to code below to do so, but the navigator.contacts.find isn't working. It doesn't return an error or a success message. If I put any type of alert after that line of code it will not appear.
function read_contacts(){
var options = new ContactFindOptions( );
options.filter = ""; //leaving this empty will find return all contacts
options.multiple = true; //return multiple results
var filter = ["displayName"]; //an array of fields to pare against the options.filter
navigator.contacts.find(filter, successFunc, errFunc, options); //breaking the code
function successFunc( matches ){
alert("reading contacts...");
for( var i=0; i<matches.length; i++){
alert( matches[i].displayName );
}
function errFunc(){
alert("Error finding contacts");
}
}
}
Share
Improve this question
asked Mar 11, 2014 at 21:10
MissElizabethMissElizabeth
5591 gold badge5 silver badges11 bronze badges
3 Answers
Reset to default 3Phonegap Docs
Try this -
function onDeviceReady() {
var options = new ContactFindOptions();
options.filter = ""; // empty search string returns all contacts
options.multiple = true; // return multiple results
filter = ["displayName", "name"]; // return contact.displayName
navigator.contacts.find(filter, 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);
}
}
// onError: Failed to get the contacts
function onError(contactError) {
alert('onError!');
}
Try this code for finding all contacts with display name.
HTML
<ol id="contact"></ol>
JAVASCRIPT
function read_contacts(){
var options = new ContactFindOptions();
options.filter="";
options.filter="";
options.multiple=true;
var fields = ["*"]; //"*" will return all contact fields
navigator.contacts.find(fields, onSuccess, onError, options);
}
// display the address information for all contacts
function onSuccess(contacts) {
//console.log(JSON.stringify(contacts))
var li = '';
$.each(contacts, function(key, value) {
if(value.name){
$.each(value.name, function(key, value) {
if(key == 'formatted'){
name = value;
}
});
}
if(value.phoneNumbers){
$.each(value.phoneNumbers, function(key, value) {
phone = value.value;
});
}
li += '<li style="text-decoration:none;">'+name+' '+phone+'</li>';
});
$("#contact").html(li);
}
function onError(contactError) {
alert('onError!');
}
you don't ask for desiredFields :
options.desiredFields = [navigator.contacts.fieldType.id, navigator.contacts.fieldType.formatted, navigator.contacts.fieldType.name, navigator.contacts.fieldType.phoneNumbers];