从 ABAddressBook 获取合并/统一条目

发布于 2024-09-30 05:44:31 字数 249 浏览 5 评论 0原文

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

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

查看联系人卡片(来自 iPhone 联系人),底部有一个名为“链接的联系人”的部分,因此显然苹果将这两个条目“合并”/“统一”到我所看到的条目中。

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

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

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-10-07 05:44:31

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

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


1.使用 ABAddressBookCopyArrayOfAllPeople 获取所有 ABPerson。 将人员存储在 allPersonRecords 数组中。

2.迭代所有 ABPerson。


2.1 获取每个 ABPerson 的链接人员列表。 使用

ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef 人);

如果没有链接的联系人,此方法将返回一个数组,其中包含引用他/她自己的人。因此,如果返回数组的 count > > 1、此人有关联联系人。

2.2 将链接的人员添加到 NSMutableSet。 这些链接的人员将被跳过,并且在以后的迭代中不会被处理。

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

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


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

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];
一绘本一梦想 2024-10-07 05:44:31

您需要查看名为的函数:

CFArrayRef ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef person);

该函数在 ABPerson.h 中定义。您传入一个人的 ABRecordRef,该函数返回一个 ABRecordRef 对象数组,表示链接到您传入的人的地址簿卡片。

  1. 制作包含返回的地址簿条目的数组的可变副本来自 ABAddressBookRef。为了方便讨论,将此新数组称为“finalContacts”。

  2. 迭代原始联系人数组。

  3. 对于数组中的每个条目,调用上述函数并传入当前条目。您将获得链接的 ABPersonRef 对象的列表。从“finalContacts”数组中删除所有这些条目。

  4. 迭代后,所有链接的卡片都应从“finalContacts”中删除,您应该留下一个唯一的地址簿卡片列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文