gpt4 book ai didi

iphone - 导入 vcf vcard 文件时的 EXC_BAD_ACESS

转载 作者:行者123 更新时间:2023-11-29 04:02:20 24 4
gpt4 key购买 nike

我正在将应用程序文档目录中的 vcf 文件中的 vcard 数据检索到字符串中。该字符串很好,但 ABPersonCreatePeopleInSourceWithVCardRepresentation 抛出 BAD_ACCESS 错误。代码如下...

ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
NSData *data=[vcardString dataUsingEncoding:NSUTF8StringEncoding];
CFDataRef vCardData = (__bridge CFDataRef)data;
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if(granted)
{
NSLog(@"%@",vcardString);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);//BAD_ACCESS ERROR
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(addressBook, person, NULL);
NSLog(@"added");
}

CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(addressBook, NULL);
}
else
NSLog(@"not granted");

这是 NSLog vcardString...

BEGIN:VCARD

VERSION:3.0

PRODID:-//Apple Inc.//iOS 6.1//EN

N:;Home;;;

FN:Home

EMAIL;type=INTERNET;type=HOME;type=pref:carljosdsdsewhnson.akasdssh@gmail.com

TEL;type=CELL;type=VOICE;type=pref:2323454645

PHOTO;ENCODING=b;TYPE=JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAICAgICAQICAgIC

....LOT OF GIBBERISH....

END:VCARD

最佳答案

您正在使用CFDataRef按要求,但内存管理不正常。完成处理程序将不会在线执行,到那时data将被解除分配。这使得vCardData悬空指针造成错误的访问。要解决此问题,请代替 __bridge使用 __bridge_retained然后调用CFReleasevCardData完成后。

ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(addressBook);
NSData *data=[vcardString dataUsingEncoding:NSUTF8StringEncoding];
CFDataRef vCardData = (__bridge_retained CFDataRef)data;
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if(granted)
{
NSLog(@"%@",vcardString);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);//BAD_ACCESS ERROR
CFRelease(vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
//...

关于iphone - 导入 vcf vcard 文件时的 EXC_BAD_ACESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15701301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com