Skip to content

Instantly share code, notes, and snippets.

@macdonst
Last active December 18, 2015 00:18
Show Gist options
  • Save macdonst/5695503 to your computer and use it in GitHub Desktop.
Save macdonst/5695503 to your computer and use it in GitHub Desktop.
How to save all your contacts from backup.
function saveAllTheContacts(contacts) {
var saveContacts = function() {
// No contacts left, stop saving
if (contacts.length == 0) {
return;
}
var contactData = contacts.pop();
// if you are restoring from a backup make sure you remove the id's
contactData.id = null;
contactData.rawId = null;
var contact = navigator.contacts.create(contactData);
contact.save(function() {
saveContacts();
}, null);
};
saveContacts();
}
@velibormrdak
Copy link

06-03 04:00:53.489: D/ContactsAccessor(12077): displayName is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): middleName is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): honorificPrefix is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): honorificSuffix is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): Could not get addresses
06-03 04:00:53.499: D/ContactsAccessor(12077): Could not get organizations
06-03 04:00:53.499: D/ContactsAccessor(12077): Could not get emails
06-03 04:00:53.499: D/ContactsAccessor(12077): note is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): nickname is string called 'null'
06-03 04:00:53.499: D/ContactsAccessor(12077): Could not get websites
06-03 04:00:53.509: D/ContactsAccessor(12077): birthday is string called 'null'
06-03 04:00:53.509: D/ContactsAccessor(12077): Could not get photos

  • - - - - - - - - - - - - - - - - - but no loop

@macdonst
Copy link
Author

macdonst commented Jun 3, 2013

saveAllTheContacts([{"name":{"givenName":"Test1","formatted":"Test","middleName":"Test","familyName":"Test","honorificPrefix":null,"honorificSuffix":null}},
{"name":{"givenName":"Test2","formatted":"Test","middleName":"Test","familyName":"Test","honorificPrefix":null,"honorificSuffix":null}},
{"name":{"givenName":"Test3","formatted":"Test","middleName":"Test","familyName":"Test","honorificPrefix":null,"honorificSuffix":null}}]);

@velibormrdak
Copy link

When I removed id, rawId... and other fields I got it working. It wasn't problem with your code, it was my JSON data (iphone export). I just created new Object with only name and phonenumbers, it is working OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment