5

I am using this code to export contact from ios phonebook to .vcf file. I have used this code for the task. But vcardString is always returning nil. Please help me to solve this issue.

NSMutableArray *contacts=[NSMutableArray alloc] init];
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (!granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
        });
        return;
    }
    NSMutableArray *contacts = [NSMutableArray array];

    NSError *fetchError;
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactIdentifierKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]]];

    BOOL success = [store enumerateContactsWithFetchRequest:request error:&fetchError usingBlock:^(CNContact *contact, BOOL *stop) {
        [contacts addObject:contact];
    }];
    if (!success) {
        NSLog(@"error = %@", fetchError);
    }

    // you can now do something with the list of contacts, for example, to show the names

    CNContactFormatter *formatter = [[CNContactFormatter alloc] init];

    for (CNContact *contact in contacts) {

        [contactsArray addObject:contact];
        // NSString *string = [formatter stringFromContact:contact];

        //NSLog(@"contact = %@", string);
    }

    //NSError *error;
    NSData *vcardString =[CNContactVCardSerialization dataWithContacts:contactsArray error:&error];

    NSLog(@"vcardString = %@",vcardString);
}];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Please read about [how to ask good questions](//stackoverflow.com/help/how-to-ask) and try to edit your question. With high quality questions you will receive better answers faster. Thanks! – Tobi Nary Jan 22 '16 at 13:25
  • What doesn't work exactly? Which line seems to be causing issue? – Larme Jan 22 '16 at 13:27
  • @Larme I have edited my question. – Vijay Bisht Jan 22 '16 at 13:39
  • You have an `NSError` parameter in `dataWithContacts:error:` class method of `CNContactVCardSerialization`. Check for its value? – Larme Jan 22 '16 at 13:40
  • @Larme : It showing this error : "Exception writing contacts to vCard (data): A property was not requested when contact was fetched" – Vijay Bisht Jan 22 '16 at 13:47
  • Use the debugger. Step through the code line by line and check each value. Is `request` valid? Does `contacts` end up with one or more contacts? Is there any error from `enumerateContactsWithFetchRequest`? Does `contactsArray` have the expected contacts in it? Why is an `NSData` variable named `vcardString`? – rmaddy Jan 22 '16 at 15:12
  • @rmaddy : There is no error in enumerateContactsWithFetchRequest and contactsArray also has array of contacts. – Vijay Bisht Jan 25 '16 at 05:43
  • Update your question with the complete and exact output of the `NSLog(@"error = %@", error);` after the call to `dataWithContacts:error:`. – rmaddy Jan 25 '16 at 05:52

1 Answers1

3

Change this line:

CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[[CNContactVCardSerialization descriptorForRequiredKeys]]

This fetches all required info for creating a vCard.

mafellows
  • 178
  • 9