I am trying to save a copy of all the entries in the address book, save it to a file as vcards, and then retrieve that file (or a version of that file) and add them back to the address book when I need them, in a backing up context. I know this question has been asked before, but for the life of me (and my newbish programming skills) I can not get it to work:
- (void)grabContacts
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSData *vcards = (NSData *)ABPersonCreateVCardRepresentationWithPeople(contacts);
[vcards autorelease];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.dat"];
[NSKeyedArchiver archiveRootObject:vcards toFile:filePath];
NSLog(@"Documents directory: %@",
[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);
CFRelease(addressBook);
}
I used the solution posted in Copying contacts from one source to another for the receiving end, but it doesn't work and due to the vague documentation on Apple's side I have wracked my brain for no result.
Thanks in advance.
S.
Nevermind. I found my problem. I did not unarchive the files when I restored them. Silly me gets what silly me deserves for programming without copious amounts of alcohol.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.