Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cordova

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

Thanks,

like image 969
Yerram Naveen Avatar asked Oct 09 '22 03:10

Yerram Naveen


1 Answers

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

like image 160
Kamran Ahmed Avatar answered Oct 12 '22 11:10

Kamran Ahmed