繁体   English   中英

Cordova联系人插件ContactFindOptions

[英]Cordova Contacts Plugin ContactFindOptions

我正在为我的应用程序使用Cordova,并且尝试使用用户的联系人填充listview。 我正在使用ContactFindOption()函数,并尝试了示例代码。

这是我的代码:

function onDeviceReady() {
    var options = new ContactFindOptions();
    options.filter="";          // empty search string returns all contacts
    options.multiple=true;      // return multiple results
    filter = ["displayName","phoneNumber"];   // return contact.displayName field

    navigator.contacts.find(filter, onSuccess, onError, options);
}

var contactsArray = [];

function onSuccess(contacts) {
    for (var i=0; i<contacts.length; i++) {
        if (contacts[i].displayName) {
            contactArray.push(contacts[i]);
        }
    }
    alert(contactsArray);
}

当我运行应用程序时,有一个控制台日志显示

Uncaught ReferenceError: ContactFindOptions is not defined.

提前致谢!

您没有为onError定义回调函数,就像使用“ onSuccess”函数所做的那样

您需要安装通讯录插件

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts.git

您已将过滤器设置为["displayName","phoneNumber"]; 这是不正确的。

有关此文档的内容有些不足。 这是您想做的。

首先,您需要为查找创建一个onError函数。 因为这是必需的功能。 您可以使用完成onSuccess函数的相同方法执行此操作。

其次,您的filter = ["displayName","phoneNumber"]; 是不正确的。 代替filter您需要声明fields = ["displayName","phoneNumber"]; 这将仅从您的联系人返回displayName和phoneNumber字段。

这是您完整的代码的外观。

 function onDeviceReady() { var options = new ContactFindOptions(); options.filter=""; // there is no need to provide this as it will return all anyway if not passed in options.multiple=true; // return multiple results var fields = ["displayName","phoneNumber"]; //return displayName and phoneNumber FIELDS navigator.contacts.find(fields, onSuccess, onError, options); } var contactsArray = []; function onSuccess(contacts) { for (var i=0; i<contacts.length; i++) { if (contacts[i].displayName) { contactArray.push(contacts[i]); //can also be done like this //contactArray[i] = contacts[i]; } } alert(contactsArray); } function onError(){ alert('there was an error finding contacts } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM