ragu ragu - 1 year ago 178
Objective-C Question

I want to take ios contact backup in .vcf file formate

Could someone explain how to take Contact backup in .vcf formate, any sample coding in iOS ?

Answer Source
#import <AddressBook/AddressBook.h>

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    ABAddressBookRef addressBook = ABAddressBookCreate();

    __block BOOL accessGranted = NO;

    if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);


    else { // We are on iOS 5 or Older
        accessGranted = YES;
        [self getContactsWithAddressBook:addressBook];

    if (accessGranted) {
        [self getContactsWithAddressBook:addressBook];

// Get the contacts.
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook

    //----------------------------------------------- create vcf file------------------------------------------

    CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
    NSString *vcardString = [[NSString alloc] initWithData:(__bridge NSData *)(vcards) encoding:NSUTF8StringEncoding];
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *folderPath = [paths objectAtIndex:0];
    NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.vcf"];
    [vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"Documents directory: %@",[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);


Try This, It will Work.