gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:53:03 25 4
gpt4 key购买 nike

使用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); }

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

最佳答案

您可以使用此方法获取.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];
}];
}

关于objective-c - 使用 Objective-C 中的 Contacts.Framework 将所有联系人导出到一个 .vcf 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com