Over a million developers have joined DZone.

Cordova (Formerly PhoneGap) - Saving a Contact in Windows Phone

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

In this article I will discuss about creating Contacts in Windows Phone using Cordova or PhoneGap.  If this is your first time use of Cordova or PhoneGap, I would recommend to read Getting Started with Cordova or PhoneGap in Windows Phone

Let's write some code to create a contact:

Step 1: Open index.html under www folder and put below code.

<!DOCTYPE html>
<html>
   <head>
      <title>Getting Started Sample</title>
      <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
      <script type="text/javascript" charset="utf-8">
         var init = function init() {
            // Wait for Cordova to load
            document.addEventListener("deviceready", onDeviceReady, false);
            // Cordova is ready
            function onDeviceReady() {
            }
         };

         window.onload = init;
      </script>
   </head>
   <body>
   </body>
</html>

Step 2: Now modify onDeviceReady method like below to save Contact phone number.

function onDeviceReady() {
   var contact = navigator.contacts.create();
   
   var phoneNumbers = [];
   phoneNumbers[0] = new ContactField('work', '111-222-3333', false);
   phoneNumbers[1] = new ContactField('mobile', '444-555-6666', true); // preferred number
   phoneNumbers[2] = new ContactField('home', '777-888-9999', false);
   
   contact.phoneNumbers = phoneNumbers;
   contact.save(onSaveSuccess, onSaveError);
}

function onSaveSuccess(contact) {
   alert("Save Success");
}

function onSaveError(contactError) {
   alert("Error = " + contactError.code);
}

Now run the application and you will notice phone number is filled in Contact details as shown below.

Cordova - Phone Number

Step 3: To save email also along with Phone Number modify onDeviceReady like below.

function onDeviceReady() {
   var contact = navigator.contacts.create();

   //Code to save phone number in Contact as discussed in step 2

   var emails = [];
   emails[0] = new ContactField('work', 'abc@outlook.com', false);
   emails[1] = new ContactField('home', 'pqr@outlook.com', true); // preferred number

   contact.emails = emails;
   contact.save(onSaveSuccess, onSaveError);
}

function onSaveSuccess(contact) {
   alert("Save Success");
}

function onSaveError(contactError) {
   alert("Error = " + contactError.code);
}

Now run the application and you will notice email is filled in Contact details as shown below.

Cordova - email

Step 4: Now to save addresses, modify onDeviceReady like below.

function onDeviceReady() {
   var contact = navigator.contacts.create();

   //Code to save phone number in Contact as discussed in step 2
   //Code to save email in Contact as discussed in step 3

   var addresses = [];
   addresses[0] = new ContactAddress(false, 'work', 'My House No', 'My Street Address', 'My Locality', 'My Region', 'My PostalCode', 'My Country');
   addresses[1] = new ContactAddress(false, 'home', 'My House No', 'My Street HAddress', 'My Locality', 'My Region', 'My PostalCode', 'My Country');

   contact.addresses = addresses;
   contact.save(onSaveSuccess, onSaveError);
}

function onSaveSuccess(contact) {
   alert("Save Success");
}

function onSaveError(contactError) {
   alert("Error = " + contactError.code);
}

Now run the application and you will notice address is filled in Contact details as shown below.

Cordova - Address

Step 5: To save organization details, url and note, put below code in onDeviceReady.

function onDeviceReady() {
   var contact = navigator.contacts.create();

   //Code to save phone number in Contact as discussed in step 2
   //Code to save email in Contact as discussed in step 3
   //Code to save address in Contact as discussed in step 4

   var organizations = [];
   organizations[0] = new ContactOrganization(false, 'work', 'OrgName', 'OrgDept', 'OrgTitle');
   contact.organizations = organizations;

   contact.note = 'PhoneGap Note';
   var urls = [];
   urls[0] = new ContactField('work', 'http://www.abc.com');
   contact.urls = urls;
   contact.save(onSaveSuccess, onSaveError);
}

function onSaveSuccess(contact) {
   alert("Save Success");
}

function onSaveError(contactError) {
   alert("Error = " + contactError.code);
}

Now run the application and you will see Website, notes and jobTitle is filled in Contact details as shown below. Organization Name appears along with the Name of Contact which you will see in next step.

Cordovs - Organization URL Note

Step 6: Now to save Contact name put below code in the onDeviceReady as shown below.

function onDeviceReady() {
   var contact = navigator.contacts.create();

   //Code to save phone number in Contact as discussed in step 2
   //Code to save email in Contact as discussed in step 3
   //Code to save address in Contact as discussed in step 4
   //Code to save email, notes and organization details in Contact as discussed in step 5

var name = new ContactName();
   name.givenName = "Jane";
   name.familyName = "FN";
   name.middleName = "MN";
   name.honorificPrefix = "Pr";
   name.honorificSuffix = "Su";

   contact.name = name;
   contact.save(onSaveSuccess, onSaveError);
}

function onSaveSuccess(contact) {
   alert("Save Success");
}

function onSaveError(contactError) {
   alert("Error = " + contactError.code);
}

Now run the appication and you will see Contact Name is filled in Contact Details.

Corodova - Contact Name

This ends the article of saving contact in windows phone using cordova or PhoneGap.

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}