DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Cordova (Formerly PhoneGap) - Saving a Contact in Windows Phone

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

Sumit Dutta user avatar by
Sumit Dutta
·
Aug. 14, 12 · Interview
Like (0)
Save
Tweet
Share
7.07K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Contacts (Apple) Windows Phone Apache Cordova

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • SAST: How Code Analysis Tools Look for Security Flaws
  • Public Cloud-to-Cloud Repatriation Trend
  • A Simple Union Between .NET Core and Python
  • Artificial Intelligence in Drug Discovery

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: