我正在使用 Phonegap 构建手机应用程序。我正在尝试使用 Cordova Contact API 来检索我手机上的联系人姓名。这是我当前的代码。它适用于 Android 设备。它只是从电话中获取联系人并将它们附加到 ul 元素。我使用了一些 jquery 来进行附加。 Cordova Contact API 中的 displayName 属性允许您获取电话上联系人的姓名。我刚刚意识到它在 ios 上不受支持。我试过 contacts[I].name;在我的代码中也是如此,但一无所获。如果有人能告诉我 ios 支持哪些属性,我将不胜感激。如何在我的iphone4 或5 上获取我的联系人姓名?
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
// find all contacts
var options = new ContactFindOptions();
options.filter="";
options.multiple=true;
var filter = ["*"];
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++) {
//alert(contacts[i].displayName);
//make if statement where
var mycontact = contacts[i].displayName;
///alert(mycontact);
if(mycontact == null){
}
else
{
$("#contactlist").append('<li style="background-color:rgb(184,249,255);height:70px;overflow:hidden;border-top:solid 1px; border-bottom:solid 1px background-color:rgb(184,249,255);"><p style="font-family: Arial;font-size: 18px;top: 5px;position: relative;left: 10px;">' + mycontact + '</p></li>');
}
}
// ele.innerHTML = str;
}
// onError: Failed to get the contacts
//
function onError(contactError) {
alert('onError!');
}
最佳答案
我今天看到一篇博文很好地解释了这一点:http://www.raymondcamden.com/index.cfm/2014/7/9/Cordova-Plugin-update-and-new-Contacts-demo
简而言之,作者在演示代码的末尾提供了以下内容:
/*
Handles iOS not returning displayName or returning null/""
*/
function getName(c) {
var name = c.displayName;
if(!name || name === "") {
if(c.name.formatted) return c.name.formatted;
if(c.name.givenName && c.name.familyName) return c.name.givenName +" "+c.name.familyName;
return "Nameless";
}
return name;
}
关于javascript - ios上使用Cordova Contact API获取联系人,不支持displayName。什么是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24620002/