Skip to content

Instantly share code, notes, and snippets.

@erwinmaza
Last active January 9, 2016 23:14
Show Gist options
  • Save erwinmaza/4656200 to your computer and use it in GitHub Desktop.
Save erwinmaza/4656200 to your computer and use it in GitHub Desktop.
ABContactsHelper modification to remove linked contacts from "all contacts" list. Preliminary testing shows the end result mimics the list in the Contacts app. Note: I have not determined if the data returned for each ABContact includes data from linked records. I suspect it does not. Most likely, the data access methods will need to be modified…
/*
This modification extends Erica Sadun's ABContactsHelper.m
https://github.com/erica/ABContactHelper
Feedback and performance suggestions welcome!
Edit 1/29/13: refactor to avoid calling ABPersonCopyArrayOfAllLinkedPeople() multiple times for same person.
Testing shows 5% performance improvement (811 total records = 249 unlinked + 562 linked),
3 run average, looping through code 10 times each run, in simulator.
*/
+ (NSArray *) contacts {
ABAddressBookRef addressBook = [ABStandin addressBook];
NSArray *thePeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *contactArray = [NSMutableArray arrayWithCapacity:thePeople.count];
NSMutableArray *linkedPeopleArrays = [NSMutableArray arrayWithCapacity:0];
for (id person in thePeople) {
NSArray *linked = (__bridge_transfer NSArray *)ABPersonCopyArrayOfAllLinkedPeople((__bridge ABRecordRef)person);
if ([linked count] <= 1) {
ABContact *contact = [ABContact contactWithRecord:(__bridge ABRecordRef)person];
[contactArray addObject:contact];
} else {
[linkedPeopleArrays addObject:linked];
}
}
NSMutableArray *peopleUsed = [NSMutableArray arrayWithCapacity:0];
for (NSArray *linkedPeople in linkedPeopleArrays) {
NSInteger personId = NSIntegerMax;
id personUsed = nil;
for (id linkedPerson in linkedPeople) {
if ([peopleUsed containsObject:linkedPerson]) continue;
ABRecordID recordID = ABRecordGetRecordID((__bridge ABRecordRef)linkedPerson);
if (recordID < personId) {
personId = recordID;
personUsed = linkedPerson;
}
[peopleUsed addObject:linkedPerson];
}
if (personUsed != nil) {
ABContact *contact = [ABContact contactWithRecord:(__bridge ABRecordRef)(personUsed)];
[contactArray addObject:contact];
}
}
return contactArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment