$cordova plugin add cordova-plugin-contacts
图 8出现如上则添加成功
<!--这是选择联系人-->
function openContacts () {
navigator.contacts.pickContact(function (contact) {
console.log('the follow contact has been selected' +JSON.stringify(contact));
},function (err) {
console.log('error' +err);
})
}选择之后返回的是一串json数据
{ "id": 3, "rawId": null, "displayName": null, "name": { "givenName": "John", "honorificSuffix": null, "formatted": "John Appleseed", "middleName": null, "familyName": "Appleseed", "honorificPrefix": null }, "nickname": null, "phoneNumbers": [ { "value": "888-555-5512", "pref": false, "id": 0, "type": "mobile" }, { "value": "888-555-1212", "pref": false, "id": 1, "type": "home" } ], "emails": [ { "value": "John-Appleseed@mac.com", "pref": false, "id": 0, "type": "work" } ], "addresses": [ { "pref": "false", "locality": "Atlanta", "region": "GA", "id": 0, "postalCode": "30303", "country": "USA", "type": "work", "streetAddress": "3494 Kuhl Avenue" }, { "pref": "false", "locality": "Atlanta", "region": "GA", "id": 1, "postalCode": "30303", "country": "USA", "type": "home", "streetAddress": "1234 Laurel Street" } ], "ims": null, "organizations": null, "birthday": "1980-06-22T12:00:00.000Z", "note": "College roommate", "photos": null, "categories": null, "urls": null}具体字段的含义参见:http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-contacts/index.html
<!--这是新建联系人-->
function createContact () {
var myContact = navigator.contacts.create({"displayName":"test user"});
myContact.save(function (contact) {
console.log('the contact has created' +json.stringify(contact));
},function (err) {
console.log('error' +err);
});
}<!--这是找寻联系人-->
function findContct () {
function onSuccess(contact) {
alert(' 找到=' +contact.length +'个联系人');
}
function onError(error) {
alert('error'+error);
}
var options = new ContactFindOptions();
options.filter = "test user";
options.multiple = true;
options.desiredFields = [navigator.contacts.fieldType.id];
options.hasPhoneNumber = false;
var fields = [navigator.contacts.fieldType.displayName,navigator.contacts.fieldType.name];
navigator.contacts.find(fields,onSuccess,onError,options);
}