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(); | |
} |
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}}]);
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
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