I have an issue with Phonegap contacts.save. When I create a contact works perfectly in iOS and Android, but, when I try to update a contact, there are duplicates fileds as phoneNumbers, emails, urls, ims, addresses, I'm using Phonegap 2.1 and Xcode 4.5.1.
Someone could help me to solve this issue?. I appreciate your time. Thanks.
In resume:
My code is:
var options = new ContactFindOptions();
options.filter = 20; //just it's an example. Looking for id 20.
var fields = ['id'];
var contact;
navigator.contacts.find(fields,function(contacts){
if (contacts.length==0)
contact = navigator.contacts.create();
else
contact = contacts[0];
var tContactName = new ContactName();
tContactName.givenName = 'Name';
tContactName.LastName = 'LastName';
contact.name = tContactName;
var tPhoneNumbers[2];
tPhoneNumbers[0] = new ContactField('work', '123456789012',true);
tPhoneNumbers[1] = new ContactField('home', '120987654321', false);
contact.phoneNumbers = tPhoneNumbers;
contact.save(function(contact) {
navigator.notification.alert('Saved sucessfully!!!',function(){},'Title');
}, function(contactError) {
navigator.notification.alert('Error contact save: '+contactError.code,function(){},'Title');
}
}, function(contactError) {
navigator.notification.alert('Error contact find: '+contactError.code,function(){},'Title');
}, options);
The code is working exactly how is should according to the W3C Contact spec. That doesn't mean it makes sense though :)
Anyway, when you create a new array of phone numbers and set that to be equal to the contact.phoneNumbers property in effect you are adding phone numbers to the contact. If you want to edit/replace the existing phone numbers you need to request the phone numbers as part of your "fields". Then you need to loop through the existing phone numbers and edit them as you see fit.
Yup, it is counter-intuitive but that's the W3C api for you.
You have to assign value to already created phone numbers contact field instead of creating new value again for the phone numbers using new ContactField.
So here is the example to update phone numbers,
contact.tphoneNumbers[0].value = 123456789012
contact.tphoneNumbers[1].value = 120987654321
then instead of using contact.phoneNumbers = tPhoneNumbers;
on update you directly call the save method and that's it.
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