Failed to create vcard contacts using contact frame

I use this code to export contacts from an iOS phonebook to a .vcf file. I used this code for the task. But vcardString always returns nil . Please help me solve this problem.

 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); }]; 
+5
source share
1 answer

Change this line:

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

Selects all the necessary information to create a business card.

+1
source

Source: https://habr.com/ru/post/1241232/


All Articles