最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - navigator.contacts.find not working (cordovaphonegap) - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 3

Phonegap 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];
发布评论

评论列表(0)

  1. 暂无评论