1. add plugin
$ cordova plugin add org.apache.cordova.contacts
2. PermissionsAndroid
app/res/xml/config.xml
<plugin name="Contacts" value="org.apache.cordova.ContactManager" />
app/AndroidManifest.xml
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
iOS
config.xml
<plugin name="Contacts" value="CDVContacts" />
3. make codescontacts.create
Returns a new Contact object.
var contact = navigator.contacts.create(properties);
Quick Example
var myContact = navigator.contacts.create({"displayName": "Test User"});
contacts.find
Queries the device contacts database and returns one or more
Contact objects, each containing the fields specified.navigator.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions);
Quick Example
function onSuccess(contacts) {
alert('Found ' + contacts.length + ' contacts.');
};
function onError(contactError) {
alert('onError!');
};
// find all contacts with 'Bob' in any name field
var options = new ContactFindOptions();
options.filter = "Bob";
options.multiple = true;
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);
Contact
Contains properties that describe a contact, such as a user's personal or business contact.
Properties
- id: A globally unique identifier. (DOMString)
- displayName: The name of this Contact, suitable for display to end-users. (DOMString)
- name: An object containing all components of a persons name. (ContactName)
- nickname: A casual name by which to address the contact. (DOMString)
- phoneNumbers: An array of all the contact's phone numbers. (ContactField[])
- emails: An array of all the contact's email addresses. (ContactField[])
- addresses: An array of all the contact's addresses. (ContactAddress[])
- ims: An array of all the contact's IM addresses. (ContactField[])
- organizations: An array of all the contact's organizations. (ContactOrganization[])
- birthday: The birthday of the contact. (Date)
- note: A note about the contact. (DOMString)
- photos: An array of the contact's photos. (ContactField[])
- categories: An array of all the user-defined categories associated with the contact. (ContactField[])
- urls: An array of web pages associated with the contact. (ContactField[])
Methods
- clone: Returns a new
Contactobject that is a deep copy of the calling object, with theidproperty set tonull. - remove: Removes the contact from the device contacts database, otherwise executes an error callback with a
ContactErrorobject. - save: Saves a new contact to the device contacts database, or updates an existing contact if a contact with the same idalready exists.
Save Quick Example
function onSuccess(contact) {
alert("Save Success");
};
function onError(contactError) {
alert("Error = " + contactError.code);
};
// create a new contact object
var contact = navigator.contacts.create();
contact.displayName = "Plumber";
contact.nickname = "Plumber"; // specify both to support all devices
// populate some fields
var name = new ContactName();
name.givenName = "Jane";
name.familyName = "Doe";
contact.name = name;
// save to device
contact.save(onSuccess,onError);
Clone Quick Example
// clone the contact object
var clone = contact.clone();
clone.name.givenName = "John";
console.log("Original contact name = " + contact.name.givenName);
console.log("Cloned contact name = " + clone.name.givenName);
Remove Quick Example
function onSuccess() {
alert("Removal Success");
};
function onError(contactError) {
alert("Error = " + contactError.code);
};
// remove the contact from the device
contact.remove(onSuccess,onError);
ContactAddress
Contains address properties for a
Contact object.Properties
- pref: Set to
trueif thisContactAddresscontains the user's preferred value. (boolean) - type: A string indicating what type of field this is, home for example. (DOMString)
- formatted: The full address formatted for display. (DOMString)
- streetAddress: The full street address. (DOMString)
- locality: The city or locality. (DOMString)
- region: The state or region. (DOMString)
- postalCode: The zip code or postal code. (DOMString)
- country: The country name. (DOMString)
4. Additional things...
No comments:
Post a Comment