Yerram Naveen
Yerram Naveen

Reputation: 286

How to add a new contact to contacts using phone gap?

I am new to Phone gap .Any one please tel me How to add a new contact to contacts using phone gap?

Thanks,

Upvotes: 5

Views: 7333

Answers (4)

Suhas Gosavi
Suhas Gosavi

Reputation: 2170

Refer Phonegap Document - Create Contact

Following is the sample code to create new contact.

var contact = navigator.contacts.create();

store contact phone numbers in ContactField[]

var phoneNumbers = [];
phoneNumbers[0] = new ContactField('work', '212-555-1234', false);
phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // preferred number
phoneNumbers[2] = new ContactField('home', '203-555-7890', false);
contact.phoneNumbers = phoneNumbers;

To save the contact

contact.save();

Upvotes: 0

Kamran Ahmed
Kamran Ahmed

Reputation: 7761

To access contacts, you need to use the contacts plugin of PhoneGap.

To add this plugin to the project all we need to do is:

cordova plugin add org.apache.cordova.contacts

To configure platform specific configuration settings we need to add the following code:

For Android: In app/res/xml/config.xml:

<feature name="Contacts">
    <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>

In app/AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

For iOS: In config.xml:

<feature name="Contacts">
    <param name="ios-package" value="CDVContacts" />
</feature>

For Windows Phone: In Properties/WPAppManifest.xml:

<Capabilities>
    <Capability Name="ID_CAP_CONTACTS" />
</Capabilities>

And to finally add a contact from through JavaScript:

var myContact = navigator.contacts.create({"displayName": "The New Contact"});
var name = new ContactName();
name.givenName = "Jane";
name.familyName = "Doe";
myContact.name = name;

var phoneNumbers = [];
phoneNumbers[0] = new ContactField('work', '212-555-1234', false);
phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // preferred number
phoneNumbers[2] = new ContactField('home', '203-555-7890', false);
myContact.phoneNumbers = phoneNumbers;

myContact.note = "Example note for the newly added contact";

myContact.save(onSuccessCallBack, onErrorCallBack);

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

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

Properties of a contact:

  • id: A globally unique identifier. (DOMString)
  • displayName: The name of this Contact, suitable for display to end users. (DOMString)
  • name: An object containing all components of a persons name. (ContactName)
  • nickname: A casual name by which to address the contact. (DOMString)
  • phoneNumbers: An array of all the contact's phone numbers. (ContactField[])
  • emails: An array of all the contact's email addresses. (ContactField[])
  • addresses: An array of all the contact's addresses. (ContactAddress[])
  • ims: An array of all the contact's IM addresses. (ContactField[])
  • organizations: An array of all the contact's organizations. (ContactOrganization[])
  • birthday: The birthday of the contact. (Date)
  • note: A note about the contact. (DOMString)
  • photos: An array of the contact's photos. (ContactField[])
  • categories: An array of all the user-defined categories associated with the contact. (ContactField[])
  • urls: An array of web pages associated with the contact. (ContactField[])

For more information PhoneGap API Documentation - Contacts

Upvotes: 4

Maulik Shah
Maulik Shah

Reputation: 421

Please have a look at http://coenraets.org/blog/cordova-phonegap-3-tutorial/,let me know if you need more help

Upvotes: 0

Hardik Patel
Hardik Patel

Reputation: 838

You can go through HELP OF PHONEGAP FOR CONTCTS. I think its good and enough documentation for add new contact to contacts.

Upvotes: 0

Related Questions