1

我找到了将 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);

知道为什么吗?另一件事,是我在其他应用程序中看到设置使用通讯录的权限,也许这就是解决方案?

4

1 回答 1

1

如果这是在 iOS 6 上,您需要征得用户的许可才能访问地址簿。

ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
   if(granted) {
      //Use the address book
   }
});

这将弹出提示,以便用户可以批准您的应用程序。

于 2012-10-16T18:02:11.313 回答