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

iOS: Crash when try to import contacts using 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];

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

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