Getting merged/unified entries from ABAddressBook

烂漫一生 提交于 2019-11-29 21:48:21
Min Tsai

To create a list of contacts that merges in linked contacts:

Note: ABPerson references are stored in custom Person class instances. All persons are then stored in a dictionary addressBookDictionary using recordID of each person as the key.


1. Get all ABPersons using ABAddressBookCopyArrayOfAllPeople. Store persons in allPersonRecords array.

2. Iterate through all ABPersons.


2.1 Get a list of linked persons for each ABPerson. Use

ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef person);

If there are no linked contacts, this method will return an array including the person reference him/herself. So if the return array has a count > 1, the person has linked contacts.

2.2 Add the linked persons to a NSMutableSet. These linked persons will be skipped and not processed in future iterations.

2.3 Create a Person instance for the current ABPerson.

2.4 Merge linked person information into Person instance. A linked person may have different phone numbers, so you need to merge them together.


NSArray *allPersonRecords = (NSArray *) ABAddressBookCopyArrayOfAllPeople(self.addressBook);
NSMutableSet *linkedPersonsToSkip = [[NSMutableSet alloc] init];

for (int i=0; i<[allPersonRecords count]; i++){

    ABRecordRef personRecordRef = [allPersonRecords objectAtIndex:i];

    // skip if contact has already been merged
    //
    if ([linkedPersonsToSkip containsObject:personRecordRef]) {
        continue;
    }

    // Create object representing this person
    //
    Person *thisPerson = [[Person alloc] initWithPersonRef:personRecordRef];

    // check if there are linked contacts & merge their contact information
    //
    NSArray *linked = (NSArray *) ABPersonCopyArrayOfAllLinkedPeople(personRecordRef);
    if ([linked count] > 1) {
        [linkedPersonsToSkip addObjectsFromArray:linked];

        // merge linked contact info
        for (int m = 0; m < [linked count]; m++) {
            ABRecordRef iLinkedPerson = [linked objectAtIndex:m];
            // don't merge the same contact
            if (iLinkedPerson == personRecordRef) {
                continue;
            }
            [thisPerson mergeInfoFromPersonRef:iLinkedPerson];
        }
    }
    [self.addressBookDictionary setObject:thisPerson forKey:thisPerson.recordID];
    [thisPerson release];
    [linked release];
}
[linkedPersonsToSkip release];
[allPersonRecords release];

You need to have a look at a function named:

CFArrayRef ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef person);

This function is defined in ABPerson.h. You pass in an ABRecordRef for a person and the function returns an array of ABRecordRef objects representing the address book cards that are linked to the person you passed in.

  1. Make a mutable copy of the array containing the address book entries that were returned from the ABAddressBookRef. For the sake of discussion, call this new array "finalContacts".

  2. Iterate over the original array of contacts.

  3. For each entry in the array, call the above function and pass in the current entry. You will get out a list of linked ABPersonRef objects. Remove all of these entries from the "finalContacts" array.

  4. After iteration, all linked cards should be removed from "finalContacts" and you should be left with a unique list of address book cards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!