我想为地址簿中的所有内容创建一个Vcf文件。目前我正在为单个联系人创建单个vcf文件。我不知道如何为通讯录中的所有联系人创建单个vcf文件。我使用以下代码创建单个vcf文件。它返回一个vcf文件。 +(NSString *)generateVCardStringWithRec:(ABRecordRef)rec { [VCard setItemCounter:0];
//
ABContact *contact = [ABContact contactWithRecord:rec];
//
NSString *vcard = @"BEGIN:VCARD\nVERSION:3.0\n";
// Name
vcard = [vcard stringByAppendingFormat:@"N:%@;%@;%@;%@;%@\n",
(contact.lastname ? contact.lastname : @""),
(contact.firstname ? contact.firstname : @""),
(contact.middlename ? contact.middlename : @""),
(contact.prefix ? contact.prefix : @""),
(contact.suffix ? contact.suffix : @"")
];
vcard = [vcard stringByAppendingFormat:@"FN:%@\n",contact.compositeName];
if( contact.nickname ) vcard = [vcard stringByAppendingFormat:@"NICKNAME:%@\n",contact.nickname];
if( contact.firstnamephonetic ) vcard = [vcard stringByAppendingFormat:@"X-PHONETIC-FIRST-NAME:%@\n",contact.firstnamephonetic];
if( contact.lastnamephonetic ) vcard = [vcard stringByAppendingFormat:@"X-PHONETIC-LAST-NAME:%@\n",contact.lastnamephonetic];
// Work
if( contact.organization || contact.department ) vcard = [vcard stringByAppendingFormat:@"ORG:%@;%@\n",(contact.organization?contact.organization:@""),(contact.department?contact.department:@"")];
if( contact.jobtitle ) vcard = [vcard stringByAppendingFormat:@"TITLE:%@\n",contact.jobtitle];
// Mail
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"email" items:contact.emailArray labels:contact.emailLabels]];
// Tel
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"phone" items:contact.phoneArray labels:contact.phoneLabels]];
// Adress
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"address" items:contact.addressArray labels:contact.addressLabels]];
// url
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"url" items:contact.urlArray labels:contact.urlLabels]];
// IM
vcard = [vcard stringByAppendingString:[VCard toVcardField:@"im" items:contact.smsArray labels:contact.smsLabels]];
// birthday
NSDate *birthday = contact.birthday;
if (birthday)
{
NSString *bday = [NSString stringWithFormat:@"%@",birthday];
NSArray *bdayArr = [bday componentsSeparatedByString:@" "];
bday = [bdayArr objectAtIndex:0];
vcard = [vcard stringByAppendingFormat:@"BDAY;value=date:%@\n",bday];
}
// Photo
NSData *imageData = contact.thumbData;//contact.imageData;
if (imageData)
{
vcard = [vcard stringByAppendingFormat:@"PHOTO;BASE64:%@\n",[imageData base64Encoding]];
}
// end
vcard = [vcard stringByAppendingString:@"END:VCARD"];
return vcard;
}`
答案 0 :(得分:2)
非常简单
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
//----------------------------------------------- create vcf file------------------------------------------
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFDataRef vcards = (CFDataRef)ABPersonCreateVCardRepresentationWithPeople(contacts);
NSString *vcardString = [[NSString alloc] initWithData:(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]);
//----------------------------------------------- create vcf file------------------------------------------
答案 1 :(得分:0)
我知道这个问题很老但是......要将多个联系人放入单个vcf文件中,您只需从文件的新行开始输出下一个联系人。只要每个联系人都在“BEGIN:VCARD”和“END:VCARD”之间,你应该没问题。