kannan
kannan

Reputation: 421

how to create contact in iphone using phonegap

I am new to iPhone development and phonegap also. Now i want to create contact in iPhone using phonegap. I got the link to create contact in iPhone with coding. But there one HTML coding with JavaScript. But when i run the coding the simulator and device show's only the HTML tag contents. I followed this below link only:

 "http://docs.phonegap.com/en/2.0.0/cordova_contacts_contacts.md.html#Contacts"

I have attach the coding and Screen Short:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Contact Example</title>

        <script type="text/javascript" charset="utf-8" src="cordova-2.0.0.js"></script>
        <script type="text/javascript" charset="utf-8">

        // Wait for Cordova to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // Cordova is ready
        //
        function onDeviceReady() {
    var myContact = navigator.contacts.create({"displayName": "Test User"});
    myContact.note = "This contact has a note.";
    navigator.contacts.save(myContact);  //HERE
    console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
}



        </script>
      </head>
      <body>
        <h1>Example</h1>
        <p>Create Contact</p>
      </body>
    </html>

Screen Short: enter image description here

Please help me to solve the issue. I have followed all the instruction from that above link. But I didn't get the solution. Thanks in advance.

Upvotes: 4

Views: 2356

Answers (2)

Sunil Dodiya
Sunil Dodiya

Reputation: 2615

var myContact = navigator.contacts.create({"displayName": "Test User"});

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {

    try {

        var contact = navigator.contacts.create();
        contact.displayName = "Plumber";
        contact.nickname = "Plumber";       //specify both to support all devices

        // populate some fields
        var name = new ContactName();
        name.givenName = "Jane";
        name.familyName = "Doe";
        contact.name = name;

        // save to device
        contact.save(function(){
            alert("Save Success");
        },function(){
            alert("Error...");
        });

    } catch(_err) {
        alert(_err)
    }

}    


    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Create Contact</p>
  </body>
</html>

OR You can find more option for contact api.

I think you have not read document. i have also edited my answers please check once again.

Description : contacts.create is a synchronous function that returns a new Contact object.

This method does not persist the Contact object to the device contacts database. To persist the Contact object to the device, invoke the Contact.save method.

http://docs.phonegap.com/en/1.0.0/phonegap_contacts_contacts.md.html#contacts.create

Upvotes: 1

Majid Laissi
Majid Laissi

Reputation: 19788

You need to save your contact.

The documentation states :

contacts.create is a synchronous function that returns a new Contact object.

This method does not persist the Contact object to the device contacts database. To persist the Contact object to the device, invoke the Contact.save method.

function onDeviceReady() {
    var myContact = navigator.contacts.create({"displayName": "Test User"});
    myContact.note = "This contact has a note.";
    navigator.contacts.save(myContact);  //HERE
    console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
}

Upvotes: 1

Related Questions