【问题标题】:navigator.contacts.find not working (cordova/phonegap)navigator.contacts.find 不工作(cordova/phonegap)
【发布时间】:2014-03-11 21:10:56
【问题描述】:

我正在尝试访问手机上的联系人。我正在使用下面的代码来执行此操作,但 navigator.contacts.find 不起作用。它不会返回错误或成功消息。如果我在该行代码之后放置任何类型的警报,它将不会出现。

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 compare 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");
    }
}

}

【问题讨论】:

    标签: javascript android cordova android-contacts contact


    【解决方案1】:

    Phonegap Docs

    试试这个 -

    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!');
    }
    

    尝试使用此代码查找具有显示名称的所有联系人。

    【讨论】:

    • 还在 config.xml 中包含与联系人相关的插件。请参阅上面的 Phonegap 文档。
    • 有趣的是,cordova 文档是错误的。一个人可以花几个小时在这些小事上。
    【解决方案2】:

    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!');
    }
    

    【讨论】:

      【解决方案3】:

      你不要求desiredFields:

      options.desiredFields = [navigator.contacts.fieldType.id, navigator.contacts.fieldType.formatted, navigator.contacts.fieldType.name, navigator.contacts.fieldType.phoneNumbers];
      

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多