javascript - ios上使用Cordova Contact API获取联系人,不支持displayName。什么是?

标签 javascript cordova

我正在使用 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/

相关文章:

javascript - 选择页面上的 2 个按钮

ios - PHONEGAP - 无法导入证书 - IOS

iphone - Phonegap 仅针对 iPhone(而非 iPad)?

javascript - API可与Postman一起使用,但无法与Javascript Fetch一起使用

javascript - Java 中的 Deflate、Javascript 中的 Inflate、Z_DATA_ERROR

javascript - 使用客户端脚本检测访问者是否在索引页面上

javascript - 使用 Node : protocol when importing Node. js 内置模块时如何修复 eslint 错误

javascript - SharePoint 2013 - Javascript - 客户端分页 - 使用 listitemcollectionposition

javascript - 如何使用phonegap检查联系人数据库中是否存在电话号码?

javascript - 从 meteor 和 Cordova 上传视频