4

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.

kannan
  • 421
  • 2
  • 11
  • 23

2 Answers2

1

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);
}
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
  • jidma, Thanks for reply. Sorry it is not working. Give me some more ideas. – kannan Sep 10 '12 at 11:30
  • @user1586650 what's not working? what do you expect and what do you actually get ? – Majid Laissi Sep 10 '12 at 11:37
  • I have edited the question under your instruction. But i got the same output from both simulator and device. Actually am expecting one contact screen and i want to add new contact from our app to device contact list (Phone Book). – kannan Sep 10 '12 at 11:58
  • @usrr1586650 In your example you have "Create Contact" and in your screenshot "Find Contact". Check that you are executing the right code. If it s the case than check that you have a new contact in your device called "The Contact". Check also the logs printed by console.log(); – Majid Laissi Sep 10 '12 at 12:09
  • Thanks for reply. Now i checked and executed correct code. But sorry this time i got "Create Contact" in simulator and device. Please help me how can i solve this issue. – kannan Sep 10 '12 at 13:38
  • @user1586650 did you check you contact list, is "Test User" there ? – Majid Laissi Sep 10 '12 at 14:00
  • The contact list is not working. I followed all the steps under your instruction. Sorry i didn't get the contact screen. Please give me any other idea. This coding is not in normal browser also. Please help me. – kannan Sep 11 '12 at 04:02
  • @user1586650 you are not supposed to get the contact list screen. According to the example, you will only see "Example" and "Create contact". And after that, using your device contact list not the application, you browse the contact list and check whether the contact has been created. That's what the example is about. – Majid Laissi Sep 11 '12 at 13:33
  • OK, Thanks for reply. Sorry for late. – kannan Sep 11 '12 at 16:53
  • @jidma problem is, on both Android and iOS, I keep having ContactError.IO_ERROR that I don't know what's going on.. But on Android the contact get saved anyway. iOS failed.. – Lionel Chan Oct 18 '12 at 13:51
  • @LionelChan check your phonegap to see whether you're using the right version for each OS (Android and iOS). – Majid Laissi Oct 18 '12 at 19:30
  • @jidma I'm using Phonegap 2.0.0 at the moment, haven't upgrade to 2.1.0 since some plugins do not have 2.1.0 yet. On both Android and iOS I receive ContactError.IO_ERROR (or 4), on iOS contact failed to save, while on Android, contact saved. Same code ;) – Lionel Chan Oct 19 '12 at 03:26
1
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

Sunil Dodiya
  • 2,605
  • 2
  • 18
  • 21
  • JDev, Thanks for reply. Sorry it is not working.Give me some more ideas. – kannan Sep 10 '12 at 11:31
  • It has to be worked. anyways.., which phonegap version are you using.? – Sunil Dodiya Sep 10 '12 at 11:51
  • Thanks for reply. Phonegap2.0 version. I have edited the question under your instruction. But i got the same output from both simulator and device. Actually am expecting one contact screen and i want to add new contact from our app to device contact list (Phone Book). – kannan Sep 10 '12 at 12:00
  • I think you have not read document. i have also edited my answers please check once again. – Sunil Dodiya Sep 10 '12 at 12:39
  • Thanks for reply. I am using phonegap2.0 version. – kannan Sep 10 '12 at 13:15
  • No.No. It is not working. Give me any other idea to create a contact. Please help me. – kannan Sep 11 '12 at 04:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16522/discussion-between-user1586650-and-jdev) – kannan Sep 11 '12 at 05:03