create new contact using phonegap

Create New Contact using Phonegap / Apache Cordova – Part 1

Want create site? Find Free WordPress Themes and plugins.
create new contact using phonegap

Apache Cordova / Phonegap contact api

This tutorial will explains you how to create new contact in your mobile using phonegap / apache cordova’s Contact plugin. Contact Plugin (https://www.npmjs.com/package/cordova-plugin-contacts) allows you to work with device contact database such as creating new contact, clone existing contact, display / get all contacts, delete existing contact.

 

How to create new contact using phonegap ?

First Create New Phonegap / Apache Cordova Project & add your platform, then add cordova contact plugin using the command lines

For Cordova  5.0+

cordova plugin add cordova-plugin-contacts

For Older Version

cordova plugin add org.apache.cordova.contacts

 

In this example, I’m going to create new contact with static contacts details

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Create Contact</title>
 <script type="text/javascript" src="cordova.js"></script>
 <script type="text/javascript" src="js/jquery.js"></script>
 <script type="text/javascript">
 $(document).ready(function() {
 $("#createContact").click(function(){
 var myContact = navigator.contacts.create(
 {
 "displayName":null,
 "name":{
 "givenName":"Sundaravel",
 "formatted":"Sundaravel MSM",
 "middleName":null,
 "familyName":"MSM",
 "honorificPrefix":null,
 "honorificSuffix":null
 },
 "nickname":null,
 "phoneNumbers":[
 {"type":"mobile","value":"+919500707757","id":0,"pref":false},
 {"type":"other","value":"+919500707757","id":1,"pref":false}
 ],
 "emails":[
 {"type":"home","value":"sundaravelit@gmail.com","id":0,"pref":false}
 ],
 "addresses":[
 {
 "postalCode":"600094",
 "type":"work",
 "id":0,
 "locality":"Indian",
 "pref":"false",
 "streetAddress":" ",
 "region":"Chennai, Tamilnadu",
 "country":"India"
 }],
 "ims":null,
 "organizations":[
 {
 "name":"Lucin Inc",
 "title":"CEO",
 "type":null,
 "pref":"false",
 "department":"Software Development"
 }],
 "birthday":null,
 "note":"My Notes",
 "categories":null,
 "urls":[
 {
 "type":"other",
 "value":"www.phonegap.co.in",
 "id":0,
 "pref":false
 }]
 }
 );
 myContact.save();
 alert("The contact, " + myContact.name.givenName + ", has been created");
 });
 });
 </script>
</head>
<body>
 <button id="createContact">Create New Contact (static)</button>
</body>
</html>

Here myContact=navigator.contacts.create() is used for creating a new contact object. After creating a new contact object, we’ve to save contact details to phone contact database using myContact.save();. If you want to create new contact dynamically https://codesundar.com/tutorials/create-new-contact-using-phonegap-apache-cordova-part-2/

Did you find apk for android? You can find new Free Android Games and apps.
  • Pedro Blanco

    Thanks for the post! I used this with a qr code reader + parse function to get the vcard data. But i find a little issue, the contact is added to my google account and not to my memory. How do i fix that?

MyMail WordPress Plugin free
Save $50 USD on Ionic Firebase Chat Source CodeEXPLORE