On ios, using the Cordova Contact API to obtain contacts, displayName is not supported. What is?
cordova, javascript
Solution
I came across a blog post today that explains this very well: http://www.raymondcamden.com/index.cfm/2014/7/9/Cordova-Plugin-update-and-new-Contacts-demo
In short, at the end of the demo code the author has provided this:
/*
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;
}
Problem
I'm building a phone app with Phonegap. I'm trying to use the Cordova Contact API to retrieve the names of Contacts on my phone. Here is my current code. It works on Android devices. It simply gets the contacts from the phone and appends them to a ul element. I use a little jquery for the appending. The displayName property in the Cordova Contact API allows for you to get the name of the Contacts on the phone. I just realized it's not supported on ios. I tried contacts[I].name; as well in my code and got nothing. I would appreciate it if someone could tell me what property is supported on ios. How can I obtain the names of my contacts on my iphone4 or 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!'); } ```