从ABAddressBook获取合并/统一条目 [英] Getting merged/unified entries from 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联系人),在底部有一个名为链接联系人的部分,所以很明显苹果合并/统一这两个条目到我看到的那个。
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 类实例中。然后将所有人存储在字典 addressBookDictionary 中,使用每个人的 recordID 作为密钥。
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获取所有ABPersons。将人员存储在allPersonRecords数组中。
1. Get all ABPersons using ABAddressBookCopyArrayOfAllPeople. Store persons in allPersonRecords array.
2。迭代所有ABPersons。
2.1获取每个ABPerson的链接人员列表。使用
ABPersonCopyArrayOfAllLinkedPeople(ABRecordRef person);
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获取合并/统一条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!