6

Does anyone have a code example of how to properly set up the parameters and use the ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering() API to get sorted subsets of the iPhone Contact list?

hotpaw2
  • 70,107
  • 14
  • 90
  • 153

2 Answers2

28

The following should work:

ABAddressBookRef aB = ABAddressBookCreate();
ABRecordRef source = ABAddressBookCopyDefaultSource(aB); // or get the source with ABPersonCopySource(somePersonsABRecordRef);
NSArray *arr = (NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(aB, source, kABPersonSortByLastName);
// you can also use kABPersonSortByFirstName instead of kABPersonSortByLastName
gerry3
  • 21,420
  • 9
  • 66
  • 74
mrueg
  • 8,185
  • 4
  • 44
  • 66
  • 4
    Note: The preferred sort order is ABPersonGetSortOrdering() - this will fill in the sort order by user preference. – n13 Feb 01 '12 at 15:57
  • 1
    And thanks, this is awesome. It's much faster than getting all contacts and sorting them in code like I've seen in many other examples. – n13 Feb 01 '12 at 17:26
  • 1
    Has anyone been able to do this in Swift? I can't seem to get it to work. – netwire Dec 19 '14 at 18:08
0

This worked for me:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *allPeople = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
j.f.
  • 3,908
  • 2
  • 29
  • 42