迭代ABAddressBookRef中的联系人

时间:2014-07-11 20:05:04

标签: ios objective-c abaddressbook

我试图遍历地址簿(ABRecordRef)中的联系人(ABAddressBookRef' s)。在尝试复制引用的kABPersonKindProperty属性时,应用程序崩溃,以检查其是否是联系人或公司联系人。

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
    if (granted) {
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        NSMutableArray *contactsArray = [[NSMutableArray alloc] init];
        NSMutableArray *companyArray = [[NSMutableArray alloc] init];

        for( int i = 0 ; i < nPeople ; i++ ) {
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
            if (ABRecordCopyValue(ref, kABPersonKindProperty) == kABPersonKindPerson) { // The app crashes on this line.
                MNContact *contact = [[MNContact alloc] initWithRecordReference:ref];
                [contactsArray addObject:contact];
            } else if (ABRecordCopyValue(ref, kABPersonKindProperty) == kABPersonKindOrganization) {
                MNCompany *company = [[MNCompany alloc] initWithRecordReference:ref];
                [companyArray addObject:company];
            }
        }

        self.hasLoadedContactsFromDevice = YES;
        _personContacts = [[NSArray alloc] initWithArray:contactsArray copyItems:YES];
        //            _companyContacts = [[NSArray alloc] initWithArray:companyArray copyItems:YES];
    } else {
        _personContacts = nil;
        _companyContacts= nil;
        self.hasLoadedContactsFromDevice = NO;
    }
    dispatch_semaphore_signal(self.sema);
});
dispatch_semaphore_wait(self.sema, DISPATCH_TIME_FOREVER);

如果我打印ref这就是我得到的

{
kCGImageSourceUseHardwareAcceleration = 1;
"kImageIOInfoHeader_alphaInfo" = 5;
"kImageIOInfoHeader_bitsPerComponent" = 8;
"kImageIOInfoHeader_bitsPerPixel" = 32;
"kImageIOInfoHeader_cacheImageBlocks" = 0;
"kImageIOInfoHeader_colorspaceModel" = 1;
"kImageIOInfoHeader_imageHeight" = 96;
"kImageIOInfoHeader_imageHeightOriginal" = 96;
"kImageIOInfoHeader_imageIndex" = 0;
"kImageIOInfoHeader_imageWidth" = 96;
"kImageIOInfoHeader_imageWidthOriginal" = 96;
"kImageIOInfoHeader_isLittleEndian" = 0;
"kImageIOInfoHeader_plugin" = "<CGImagePlus 0x165846f0>{w=96 h=96 prov=0x0 props=0x16584110 metadata=0x16584760}";
"kImageIOInfoHeader_pluginHandlesReMapping" = 1;
"kImageIOInfoHeader_rowBytes" = 384;
"kImageIOInfoHeader_session" = "<CGImageReadSessionRef 0x165844f0>{offset=3453 readref=0x165841f0}";
"kImageIOInfoHeader_supportsMultipleResolutions" = 1;
}

所以我的想法是做一个例程检查ref是否是类ABRecordRef的实例。

谁能告诉我自己做错了什么?欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

问题在于

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

allPeople的计数不具有相同数量的元素(!= nPeople)。不知道为什么?但当我将其改为以下内容时,一切正常。

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = CFArrayGetCount(allPeople);