iOS:尝试使用ABAddressBook()导入联系人时崩溃

时间:2016-08-19 14:52:11

标签: ios objective-c abaddressbook

我正在尝试使用ABAddressBook将vcf文件导入到我的地址簿中。

首先,我读取文件,然后获取NSData:

    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"VZAllContactBackup.vcf"];

    NSData *vcardData = [[NSFileManager defaultManager] contentsAtPath:fileName];

    NSInvocationOperation *newoperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(importVcardData:) object:vcardData];
    [serailPhotoQueue addOperation:newoperation];

然后,尝试使用以下代码导入它们:

- (void) importAllVcard: (NSData *)VcardData {

    ABAddressBookRef book = ABAddressBookCreate();
    ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
    CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, (__bridge CFDataRef)(VcardData));

    numberOfContacts = CFArrayGetCount(vCardPeople);

    for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
        @autoreleasepool {
            ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
            ABAddressBookAddRecord(book, person, NULL);
        } 
    }

    CFRelease(vCardPeople);
    CFRelease(defaultSource);
    ABAddressBookSave(book, NULL);
    CFRelease(book);
}

这个逻辑在测试时对我来说很好,但不知何故,我收到了很多崩溃:

numberOfContacts = CFArrayGetCount(vCardPeople); // line 76

以下崩溃报告:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000000
Triggered by Thread:  11

...

Thread 11 name:
Thread 11 Crashed:
0   CoreFoundation                  0x0000000181590508 CFArrayGetCount + 36
1   contenttransfer                 0x000000010007ad44 -[VZContactsImport importAllVcard:] (VZContactsImport.m:76)
2   contenttransfer                 0x000000010007ad44 -[VZContactsImport importAllVcard:] (VZContactsImport.m:76)
3   contenttransfer                 0x00000001000865fc -[VZBonjourReceiveDataVC importVcardData:] (VZBonjourReceiveDataVC.m:2228)
4   CoreFoundation                  0x00000001816b8ae0 __invoking___ + 144
5   CoreFoundation                  0x00000001815b0548 -[NSInvocation invoke] + 284
6   Foundation                      0x000000018206c9c4 -[NSInvocationOperation main] + 40
7   Foundation                      0x0000000181faeed8 -[__NSOperationInternal _start:] + 604
8   Foundation                      0x000000018206e904 __NSOQSchedule_f + 224
9   libdispatch.dylib               0x00000001810fd47c _dispatch_client_callout + 16
10  libdispatch.dylib               0x00000001811094c0 _dispatch_queue_drain + 864
11  libdispatch.dylib               0x0000000181100f80 _dispatch_queue_invoke + 464
12  libdispatch.dylib               0x000000018110b390 _dispatch_root_queue_drain + 728
13  libdispatch.dylib               0x000000018110b0b0 _dispatch_worker_thread3 + 112
14  libsystem_pthread.dylib         0x0000000181315470 _pthread_wqthread + 1092
15  libsystem_pthread.dylib         0x0000000181315020 start_wqthread + 4

好像我读的vcardData是空的?或者当我试图写这个vcf文件时,它失败了?

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"VZAllContactBackup.vcf"];

[[NSFileManager defaultManager] removeItemAtPath:fileName error:nil];
[receivedData writeToFile:fileName atomically:NO];

或任何其他情况可能导致此问题?

1 个答案:

答案 0 :(得分:0)

看起来ABPersonCreatePeopleInSourceWithVCardRepresentation返回NULL,因此您在使用NULL取消引用的CFArrayGetCount()调用时崩溃。您需要检查NULL。不确定是否有任何方法可以确定它返回NULL的原因。也许该文件不存在(磁盘空间太低而无法写入?),因此您传入了nil数据。或者您尝试在Apple仍然在磁盘上加密文件时执行此操作。 ; s文件保护。或者其他许多原因。