0

我正在将应用程序文档目录中的 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
4

1 回答 1

1

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++) {
        //...
于 2013-03-29T10:21:00.490 回答