【发布时间】:2011-12-21 09:27:00
【问题描述】:
我正在尝试保存通讯簿中所有条目的副本,将其作为电子名片保存到文件中,然后检索该文件(或该文件的一个版本)并将它们添加回通讯簿,当我在备份上下文中需要它们。我知道以前有人问过这个问题,但是对于我的生活(以及我的新手编程技能)来说,我无法让它发挥作用:
- (void)grabContacts
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSData *vcards = (NSData *)ABPersonCreateVCardRepresentationWithPeople(contacts);
[vcards autorelease];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
NSString *filePath = [folderPath stringByAppendingPathComponent:@"contacts.dat"];
[NSKeyedArchiver archiveRootObject:vcards toFile:filePath];
NSLog(@"Documents directory: %@",
[fileMgr contentsOfDirectoryAtPath: folderPath error:&error]);
CFRelease(addressBook);
}
我使用Copying contacts from one source to another 中发布的解决方案作为接收端,但它不起作用,并且由于 Apple 方面的文档含糊不清,我绞尽脑汁没有结果。
提前致谢。
S.
【问题讨论】:
标签: iphone ios addressbook abaddressbook vcf-vcard