Last active
December 18, 2015 00:18
-
-
Save macdonst/5695503 to your computer and use it in GitHub Desktop.
How to save all your contacts from backup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
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
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}}]);