在 iOS 5.0/XCode 4.2 中使用 VCards 和 ABAddressBook
我正在尝试保存地址簿中所有条目的副本,将其另存为 vcards 文件,然后检索该文件(或该文件的版本)并在需要时将它们添加回地址簿,在备份上下文中。我知道这个问题以前曾被问过,但对于我的生活(以及我的新手编程技能),我无法让它发挥作用:
- (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);
}
我使用了 将联系人从一个源复制到另一个源以供接收端使用,但它不起作用,并且由于苹果方面的文档含糊不清,我已经破坏了我的脑子没有结果。
提前致谢。
S。
I am trying to save a copy of all the entries in the address book, save it to a file as vcards, and then retrieve that file (or a version of that file) and add them back to the address book when I need them, in a backing up context. I know this question has been asked before, but for the life of me (and my newbish programming skills) I can not get it to work:
- (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);
}
I used the solution posted in Copying contacts from one source to another for the receiving end, but it doesn't work and due to the vague documentation on Apple's side I have wracked my brain for no result.
Thanks in advance.
S.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。我发现了我的问题。当我恢复文件时,我没有取消归档这些文件。愚蠢的我在没有大量酒精的情况下得到了愚蠢的我应得的编程。
Nevermind. I found my problem. I did not unarchive the files when I restored them. Silly me gets what silly me deserves for programming without copious amounts of alcohol.