Reputation: 286
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
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
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:
For more information PhoneGap API Documentation - Contacts
Upvotes: 4
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
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