使用 Objective-C 中的 Contacts.Framework 将所有联系人导出到一个 .vcf 文件中

Omk*_*hav 2 vcf-vcard objective-c contacts addressbook

使用 I AddressBook.framework 我曾经从所有联系人创建 Contacts.vcf 并将其保存在文档目录中。这是我曾经使用的代码:

ABAddressBookRef addressBook1 = ABAddressBookCreate();
NSArray *arrayOfAllPeople = (__bridge_transfer NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook1);
long cnt = (unsigned long)[arrayOfAllPeople count];
if (cnt==0) {

    ABAddressBookRequestAccessWithCompletion(addressBook1, nil);

}

if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
    ABAddressBookRef addressBook2 = ABAddressBookCreate();


    CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook2);
    CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);


    NSString *vcardString = [[NSString alloc] initWithData:(__bridge NSData *)vcards encoding:NSUTF8StringEncoding];
    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];
    CFRelease(addressBook2); }
Run Code Online (Sandbox Code Playgroud)

如何使用 Contacts.framework 创建包含所有设备联系人的 Contacts.vcf 文件并将其保存在文档目录中?

小智 5

您可以使用此方法获取 .vcf 文件中的所有联系人。它返回的输出与您使用 AddressBook.framework 获得的输出相同。

- (void)getContacts {
    NSMutableArray *contactsArray=[[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:@[[CNContactVCardSerialization descriptorForRequiredKeys], [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];

        NSString* vcardStr = [[NSString alloc] initWithData:vcardString encoding:NSUTF8StringEncoding];
        NSLog(@"vcardStr = %@",vcardStr);

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *folderPath = [paths objectAtIndex:0];
        NSString *filePath = [folderPath stringByAppendingPathComponent:@"Contacts.vcf"];
        [vcardStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }];
}
Run Code Online (Sandbox Code Playgroud)