You can easily access the contact information stored on a device using the PhoneGap API. The Contacts API is an implementation of the W3C's Pick Contacts Intent API (an intent that enables access to a user's address book service from inside a web application). You can read more about the W3C specifications at http://www.w3.org/TR/contacts-api/).
In order to allow your app to access and manipulate the contacts stored on the device, you have to update the specific platform configuration files. The following table lists the information you need to add to the configuration files for Android, iOS, and Windows Phone apps. For a complete list of all the supported platforms refer to the online documentation at http://docs.phonegap.com/en/edge/cordova_contacts_contacts.md.html#Contacts.
Platform |
Configuration file |
Content |
---|---|---|
Android |
|
<plugin name="Contacts" value="org.apache.cordova.ContactManager" /> |
|
<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 |
|
<plugin name="Contacts" value="CDVContacts" /> |
Windows Phone |
|
<Capabilities> <Capability Name="ID_CAP_CONTACTS" /> </Capabilities> |
In order to start to interact with the device contacts, you can use the create
or the find
methods defined in the contacts
object stored in the navigator
one:
var contact = navigator.contacts.create(properties); navigator.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions);
In order to better understand how these methods work, let's explore the most relevant objects that are involved with them: Contact
, ContactName
, ContactField
, ContactFindOptions
, and ContactError
.
The ContactName
object is used in the PhoneGap framework in order to store all the details of a contact name. The object is stored in the property name
of the object Contact
.
The properties of the ContactName
object are all strings and are self-explanatory:
formatted
represents the complete name of the contact.familyName
represents the contact's family name.givenName
represents the contact's given name.middleName
represents the contact's middle name.honorificPrefix
represents the contact's prefix (e.g., Mr. or Dr.).honorificSuffix
represents the contact's suffix (e.g., Esq.).The ContactField
object is a generic object used in the PhoneGap framework in order to represent a field of the Contact
object. The generic nature of this object makes it reusable across several fields.
The properties of the ContactFiled
object are:
The ContactAddress
object is the object stored in the addresses
property of the Contact
object. The addresses
property is an array so that multiple addresses can be associated with each contact.
The properties of the ContactAddress
object are:
pref
: a Boolean that indicates whether the returned ContactAddress
is the preferred value of the user for the ContactAddress
objecttype
: a string that indicates what type of address is stored in the ContactAdress
object (for example, home, and office)formatted
: a string that represents the complete address formatted for displaystreetAddress
: a string that represents the complete street addresslocality
: a string that represents the city or locality that is part of the ContactAddress
objectregion
: a string representing the state or region that is part of the ContactAddress
objectpostalCode
: a string that represents the zip code or postal code associated with the locality stored in the ContactAddress
objectcountry
: a string representing the name of the country stored in the ContactAddress
objectThe
ContactOrganization
object represents all the details of a company, organization, and so on that the stored Contact
belongs to. The object is stored in the array contained in the organizations
property of the Contact
object.
The properties of the ContactOrganization
object are:
pref
: a Boolean that indicates if the returned ContactOrganization
object is the preferred value of the user for the ContactOrganization
objecttype
: a string that indicates what type of address is stored in the ContactOrganization
object (for example, work and other)name
: a string that represents the name of the organization stored in the ContactOrganization
objectdepartment
: a string that represents the department of the organization the contact works fortitle
: a string that represents the contact's title in the organizationThe properties name
, department
, and title
are partially supported on iOS; the pref
and type
properties are badly supported on Android 1.x and Android 2.x.
The Contact
object represents all the details of a contact stored in the device database. A Contact
object can be saved, removed, and copied from the device contact database using the methods save
, remove
, and clone
defined on the object itself. The save
and remove
methods accept two arguments in order to handle the success and the failure of the save or remove operation:
var contact = navigator.contacts.create({'displayName': 'Giorgio'}); contact.save(onContactSaved,onContactSavedError);contact.remove(onContactRemoved, onContactRemovedError);
The error handlers receive the same ContactError
object as an argument; the success handlers receive the saved contact or a snapshot of the current database when a contact is successfully removed.
The ContactError
object contains in the code
property the information about the occurred error. The values that can be returned are:
ContactError.UNKNOWN_ERROR
ContactError.INVALID_ARGUMENT_ERROR
ContactError.TIMEOUT_ERROR
ContactError.PENDING_OPERATION_ERROR
ContactError.IO_ERROR
ContactError.NOT_SUPPORTED_ERROR
ContactError.PERMISSION_DENIED_ERROR
When creating a new Contact
object, you can define one by one the contact properties or pass them as an object when calling the create
method. The properties of the Contact
object are:
id
: a string used as a globally unique identifierdisplayName
: a string that represents the name of the Contact
object for display to end usersname
: an object containing all the information of a contact name; the object used to store this information is the ContactName
nickname
: a string that represents the casual name of a contactphoneNumbers
: an array of all the contact's phone numbers; the array items are instances of the ContactField
objectemails
: an array of all the contact's e-mail addresses; the array items are instances of the ContactField
objectaddresses
: an array of all the contact's addresses; the array items are instances of the ContactAddresses
objectims
: an array of all the contact's instant messages accounts; the array items are instances of the ContactField
objectorganizations
: an array of all the organizations the contact belongs to; the array items are instances of the ContactOrganization
objectbirthday
: a Date
object that represents the birthday of the contactnote
: a string that represents a note about the contactphotos
: an array of all the contact's photos; the array items are instances of the ContactField
objectcategories
: an array of all the contact's defined categories; the array items are instances of the ContactField
objecturls
: an array of all the web pages associated with the contact; the array items are instances of the ContactField
objectThe Contact
object properties are not fully supported across all platforms. In fact operating system fragmentation makes it difficult to handle this information consistently.
For instance, the properties name
, nickname
, birthday
, photos
, categories
, and urls
are not supported on Android 1.x. Likewise the categories
property is supported neither on Android 2.x. nor on iOS.
On iOS, the items returned in the photos array contain a URL that points to the app's temporary folder. This means that this content is deleted when the app exits and that you have to handle it if you want the user to find the app in the same status he/she left it. The displayName
property is not supported on iOS and will be returned as null unless there is no ContactName
defined. If the ContactName
object is defined, then a composite name or nickname is returned.
I already mentioned the find
method available on the navigator.contacts
object. Using this method an app can find one or multiple contacts in the device's contact database. The find
method accepts four arguments. The first one is an array that contains the name of the fields of the Contact
object that have to be returned. The second and the third are the success and error handlers. The last one represents the filtering options you want to apply to the current research.
In order to apply a filter to the current search, it is enough to instantiate a new ContactFindOptions
object and populate the filter
and multiple
properties. The filter
property is a case-insensitive string that will act as a filter on the fields of the Contact
objects returned by the find
method. The multiple
property is false
by default and is the one to use in order to receive multiple Contact
objects in the success handler.
18.118.227.69