Skip to content

Instantly share code, notes, and snippets.

@tfanme
Created July 20, 2013 23:40
Show Gist options
  • Select an option

  • Save tfanme/6046866 to your computer and use it in GitHub Desktop.

Select an option

Save tfanme/6046866 to your computer and use it in GitHub Desktop.
PhoneGap 3.x Contacts API Example
/* Translation of property type labels contact API ---> iPhone
*
* phone: work, home, other, mobile, fax, pager -->
* kABWorkLabel, kABHomeLabel, kABOtherLabel, kABPersonPhoneMobileLabel, kABPersonHomeFAXLabel || kABPersonHomeFAXLabel, kABPersonPhonePagerLabel
* emails: work, home, other ---> kABWorkLabel, kABHomeLabel, kABOtherLabel
* ims: aim, gtalk, icq, xmpp, msn, skype, qq, yahoo --> kABPersonInstantMessageService + (AIM, ICG, MSN, Yahoo). No support for gtalk, xmpp, skype, qq
* addresses: work, home, other --> kABWorkLabel, kABHomeLabel, kABOtherLabel
*
*
*/
// Create a new Contact
function contact_create() {
var nc = navigator.contacts.create();
// id (DOMString)
nc.id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
// displayName (DOMString)
nc.displayName = "Al Pcaino";
// name (ContactName)
var name = new ContactName();
name.formatted = "艾尔弗兰德.帕西诺";
name.familyName = "Pacino";
name.middleName = "James";
name.givenName = "Alfredo";
name.honorificPrefix = "Mr.";
name.honorificSuffix = "Esq.";
nc.name = name;
// nickname (DOMString)
nc.nickname = "艾尔.帕西诺";
// phoneNumbers (ContactField Array)
var phoneNumbers = [];
phoneNumbers.push(new ContactField('work', '010-50505050', false));
phoneNumbers.push(new ContactField('mobile', '13901168061', false));
phoneNumbers.push(new ContactField('home', '010-51515151', false));
phoneNumbers.push(new ContactField('pager', '13901168061', false));
phoneNumbers.push(new ContactField('other', '010-51515151', false));
nc.phoneNumbers = phoneNumbers;
// emails (ContactField Array)
var emails = [];
emails[0] = new ContactField('work', 'tfan@muuzii.com', false);
emails[1] = new ContactField('home', 'princetoad@126.com', false);
emails[2] = new ContactField('other', 'princetoad@gmail.com', true);
nc.emails = emails;
// addresses (ContactAddress Array)
var addresses = [];
var address = new ContactAddress();
address.pref = true;
address.type = 'work';
address.formatted = "北京市朝阳区光华路7号汉威大厦";
address.streetAddress = "光华路7号";
address.locality = "北京";
address.region = "北京市";
address.postalCode = "100004";
address.country = "中国";
addresses.push(address);
var home_address = new ContactAddress();
home_address.pref = false;
home_address.type = 'home';
home_address.formatted = "";
home_address.streetAddress = "长安8号";
home_address.locality = "北京";
home_address.region = "北京市";
home_address.postalCode = "100022";
home_address.country = "中国";
addresses.push(home_address);
var other_address = new ContactAddress();
other_address.pref = false;
other_address.type = 'other';
other_address.formatted = "";
other_address.streetAddress = "建外大街9号";
other_address.locality = "北京";
other_address.region = "北京市";
other_address.postalCode = "100001";
other_address.country = "中国";
addresses.push(other_address);
nc.addresses = addresses;
// ims (ContactField Array)
var ims = [];
var qq = new ContactField('qq', '50506711', false);
var skype = new ContactField('skype', 'princetoad2008', true);
var aim = new ContactField('aim', 'princetoad2010', false);
var msn = new ContactField('msn', 'princetoad@hotmail.com', false);
ims.push(qq);
ims.push(skype);
ims.push(aim);
ims.push(msn);
nc.ims = ims;
// organizations (ContactOrganization Array)
var organizations = [];
var org = new ContactOrganization();
//org.pref = true;
//org.type = 'work';
org.name = "星巴克";
org.department = "云南咖啡项目";
org.title = "咖啡农艺师";
organizations.push(org);
// var org2 = new ContactOrganization();
// org2.pref = false;
// org2.type = 'home';
// org2.name = "山姆会员店";
// org2.department = "服装部";
// org2.title = "销售专员";
// organizations.push(org2);
// nc.organizations = organizations;
// birthday (Date)
var birthday = new Date(1981, 0, 7);
nc.birthday = birthday;
// note (DOMString)
var note = "测试一下 Contact API";
nc.note = note;
// photos (ContactField Array)
// var photos = [];
// photos.push(new ContactField('avarta','', true));
// photos.push(new ContactField('test', '', false));
// nc.photos = photos;
// categories (ContactField Array)
// var categories = [];
// categories.push(new ContactField('Business', 'test business', false));
// categories.push(new ContactField('Test', 'test group', true));
// nc.categories = categories;
// urls (ContactField Array)
var urls = [];
urls.push(new ContactField('home', 'http://www.tfan.org', true));
urls.push(new ContactField('work', 'www.zealware.net', false));
urls.push(new ContactField('other', 'www.zudm.net', false));
nc.urls = urls;
function onSaveSuccess(contact) {
console.dir(contact);
}
function onSaveError(error) {
console.dir(error);
}
nc.save(onSaveSuccess, onSaveError);
}
function onSuccess(contacts) {
for (var i=0; i < contacts.length; i++) {
console.log("Display Name = " + contacts[i].displayName, "nickname = " + contacts[i].nickname);
}
}
function onError(contactError) {
alert('onError:' + contactError.code);
}
function contact_find() {
var options = new ContactFindOptions();
options.filter = "建外";
options.multiple = true;
var fields = ["displayName", "name", "nickname", "addresses"];
navigator.contacts.find(fields, onSuccess, onError, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment