I am new to Phone gap .Any one please tel me How to add a new contact to contacts using phone gap?
Thanks,
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With