4

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.

1 Answer 1

0

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.

1
  • Congrats on the solution! When you are able, please make sure to mark your answer as 'accepted' so that others might learn from your success. Cheers~ Dec 22, 2011 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.