Contacts

This feature enables your mobile application to create, remove, and search through contacts on the mobile device.

Supported Platforms

This feature is supported in the following platforms:

  • iOS
  • Android

Installation

Execute the following command in your Cordova project directory:

cordova plugin add cordova-plugin-contacts

Usage

Create Contact

Assume that there is a "Create Contact" button on your mobile application. A new contact is created when that button is clicked. The following is a sample code snippet that can be used in the listener JS file to create a new contact:

/* The following function creates a new contact on the mobile device.
*  Here, CFEC.CLICK is the Canvas event that can be used to capture the button clicked action.
*  CREATE is the form item ID for the "CREATE CONTACT" button.
*/
this.fm.registerHandler(CFEC.CLICK, "CREATE", 
                        function (fm, event, fieldName, value) 
						{
            				var params = 
							{
                				"displayName": "CT",
								"firstName": "Canvas",
								"lastName": "Technology",
								"workNumber": "212-555-1234",
								"mobileNumber": "917-555-5432",
								"homeNumber": "2345234567"
            				};
            				var contact = canvas.env.contacts.create(params, onSuccess, onError);
 						});

// The following function is executed when new contact is successfully created on the mobile device.
function onSuccess(success) 
{
    showAlert("saved" + success);
}

// The following function is executed when new contact is not created on the mobile device.
function onError(err) 
{
    showAlert("not saved" + err);
}

View Contact

/* The following function picks a phone number from the contact on the mobile device.
*  Here, CFEC.CLICK is the Canvas event that can be used to capture the button clicked action.
*  PICK_PHONENO is the form item ID for the "PICK PHONE NUMBER" button.
*/
this.fm.registerHandler(CFEC.CLICK, "PICK_PHONENO", 
                        function (fm, event, fieldName, value) 
						{
            				canvas.env.contacts.pickPhoneNumbers(function (phoneNumber) 
							{
                				showAlert(phoneNumber);
            				}, function (err) 
							{
                				showAlert(err);
            				});
						});

/* The following function picks a name from the contact on the mobile device.
*  Here, PICK is the form item ID for the "PICK" button.
*/
this.fm.registerHandler(CFEC.CLICK, "PICK", 
                        function (fm, event, fieldName, value) 
						{
            				canvas.env.contacts.pickContact(function (message) 
							{
                				showAlert(cbx.encode(message));
            				}, function (err) 
							{
                				showAlert(err);
            				});
						});