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

Bra*_*don 3 javascript cordova

我正在使用Phonegap构建电话应用程序。我正在尝试使用Cordova联系人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!');
  }
Run Code Online (Sandbox Code Playgroud)

Daw*_*don 5

我今天遇到了一篇博客文章,对此进行了很好的解释: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;
}
Run Code Online (Sandbox Code Playgroud)