我找到了一个将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);
知道为什么吗? 另一件事是,我在其他应用程序中看到设置权限使用地址簿,也许这就是解决方案?
答案 0 :(得分:1)
如果是在iOS 6上,您需要获得用户的许可才能访问该地址簿。
ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
if(granted) {
//Use the address book
}
});
这会显示提示,以便用户批准您的应用。