从 ABAddressBook 获取合并/统一的条目

Getting merged/unified entries from ABAddressBook(从 ABAddressBook 获取合并/统一的条目)

本文介绍了从 ABAddressBook 获取合并/统一的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个显示 iPhone 联系人的应用程序.

I'm developing an application that is showing the iPhone contacts.

ABAddressBookRef 返回在 iPhone 联系人应用程序中只出现一次的联系人的重复条目.

The ABAddressBookRef returns duplicate entries for a contact that appears only once in the iPhone contacts application.

查看联系人卡片(来自 iPhone 联系人),底部有一个名为Linked Contacts"的部分,因此显然苹果将这两个条目合并"/统一"到我看到的那个中.

Looking on the contact card (from the iPhone contacts), in the bottom there is a section called "Linked Contacts" so obviously apple "merge"/"unify" these two entries into the one i see.

这里的问题是模仿相同行为的最佳方法是什么,这样我的应用程序将只显示一个条目?是否有从通讯录返回合并/统一条目的 API?

The question here is what is the best way to mimic the same behavior so my app will show only one entry? is there an API that returns the merged/unified entries from the address book?

推荐答案

创建一个合并到链接联系人中的联系人列表:

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

注意:ABPerson 引用存储在自定义 Person 类实例中.然后使用每个人的 recordID 作为键,将所有人员存储在字典 addressBookDictionary 中.

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.使用 ABAddressBookCopyArrayOfAllPeople 获取所有 ABPerson. 将人员存储在 allPersonRecords 数组中.

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

<强>2.遍历所有 ABPerson.

2.1 获取每个 ABPerson 的关联人员列表. 使用

ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef 人);

ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef person);

如果没有链接的联系人,此方法将返回一个包含引用他/她自己的人的数组.因此,如果返回数组的计数 > 1,则此人已链接联系人.

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 将链接的人员添加到 NSMutableSet. 这些链接的人员将被跳过,并且不会在以后的迭代中处理.

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

2.3 为当前的 ABPerson 创建一个 Person 实例.

2.4 将链接的人信息合并到Person实例中.链接的人可能有不同的电话号码,因此您需要将它们合并在一起.

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];

这篇关于从 ABAddressBook 获取合并/统一的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从 ABAddressBook 获取合并/统一的条目

基础教程推荐