我找到了将 vcard 保存到设备的代码。此代码在模拟器上运行完美,但在设备上尝试代码时,我看不到新联系人。代码是:
NSString *vCardString = @"BEGIN:VCARD\nVERSION:2.1\nN:Standard11.3,Joe;\nADR:PO Box 555,Suite 55,5555 Any Street,San Diego,CA,92111,USA;;\nEND:VCARD";
CFDataRef vCardData = (CFDataRef)CFBridgingRetain([vCardString dataUsingEncoding:NSUTF8StringEncoding]); ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
CFRelease(vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(book, person, NULL);
//CFRelease(person);
}
CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
知道为什么吗?另一件事,是我在其他应用程序中看到设置使用通讯录的权限,也许这就是解决方案?