03.06.12
Example of Creating a full contact with PhoneGap
I was looking around for this example, so once I figured it out I thought I’d post it here for others.
It took me a while to figure out how to create a full contact for the IPhone using phonegap, as all the existing examples are very basic and give examples showing how to create a contact with a name and
Basically I found out that the input format is the same as the output format. Simply dump a contact to the console as a JSON string. Make sure it has all the fields you are interested in.
function find() {
// find all contacts with 'Mollusc' in any name field
var options = new ContactFindOptions();
options.filter="Mollusc";
options.multiple = false;
var fields = ["*"];
navigator.contacts.find(fields, onSuccess, onError, options);
}
function onSuccess(contacts) {
console.log(JSON.stringify(contacts));
}
To create a contact you do the following.
function createContact(){
var myContact = navigator.contacts.create({"displayName":null,"name":{"givenName":"Intellectual","formatted":"Intellectual Mollusc","middleName":null,"familyName":"Mollusc","honorificPrefix":null,"honorificSuffix":null},"nickname":null,"phoneNumbers":[{"type":"other","value":"00353 2345235","id":0,"pref":false},{"type":"mobile","value":" ","id":1,"pref":false}],"emails":[{"type":"home","value":"work@intellectualmollusc.net","id":0,"pref":false}],"addresses":[{"postalCode":"","type":"work","id":0,"locality":"cork","pref":"false","streetAddress":" ","region":" ","country":"Ireland"}],"ims":null,"organizations":[{"name":"School","title":"Student","type":null,"pref":"false","department":"Kitchen"}],"birthday":null,"note":"YourRefUniqueID:47831","categories":null,"urls":[{"type":"other","value":"intellectualmollusc.net","id":0,"pref":false}]});
myContact.save();
console.log("The contact, " + myContact.name.givenName + ", has been created");
}
Reasonably Straightforward.