Cordova (Formerly PhoneGap) - Saving a Contact in Windows Phone
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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.
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.
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.
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.
This ends the article of saving contact in windows phone using cordova or PhoneGap.
Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Microservices: Quarkus vs Spring Boot
-
8 Data Anonymization Techniques to Safeguard User PII Data
-
DevOps Midwest: A Community Event Full of DevSecOps Best Practices
-
Implementing RBAC in Quarkus
Comments