문제

Me need to find out source of ABRecordRef, because the method bellow returns array of contacts from phone book and icloud and as result there are double contacts with the same person (fierst, last name), but with the different ABRecordID recordId=ABRecordGetRecordID(record);?

ABAddressBookRef addressBook;
if ([self isABAddressBookCreateWithOptionsAvailable]) {
    CFErrorRef error = nil;
    addressBook = ABAddressBookCreateWithOptions(NULL,&error);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

        dispatch_async(dispatch_get_main_queue(), ^{

            if (error) {

            } else if (!granted) {

            } else {
                CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
                CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

                for (int i = 0; i < nPeople; i++) {
                    // Get the next address book record.
                    ABRecordRef record = CFArrayGetValueAtIndex(allPeople, i);

                    ABRecordID recordId=ABRecordGetRecordID(record);

                    NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty);
                    NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty);

                    CFTypeRef bDayProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty);

                    NSDate *birthday;

                    if (ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty))
                    {
                        birthday=(__bridge NSDate*)bDayProperty;
                    }

                }
                CFRelease(addressBook);

            }
        });
    });
} else {
    // iOS 4/5
    addressBook = ABAddressBookCreate();
    CFRelease(addressBook);
} 
도움이 되었습니까?

해결책

Source of the answer

add new set

NSMutableSet *linkedPersonsToSkip = [[NSMutableSet alloc] init];

and just check for each person

if ([linkedPersonsToSkip containsObject:(__bridge id)(record)]) {
                            continue;
}

NSArray *linked = (__bridge NSArray *) ABPersonCopyArrayOfAllLinkedPeople(record);
if ([linked count] > 1) {
   [linkedPersonsToSkip addObjectsFromArray:linked];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top