2

for an iPhone project I'm using the ABContactsHelper by Erica Sadun. To show the contacts I wan't to use the same sort order as iOS uses in e.g. AdressBook.app. Is there a possibility to get this information?

I tried:

+ (BOOL) firstNameSorting;
+ (BOOL) lastNameSorting;

from ABContactsHelper class but these always give me first name sorting. Even if I set lastname,firstname in contact setting. Thanks in advance.

Chris

crizztus
  • 307
  • 3
  • 10

1 Answers1

5

ABPersonGetSortOrdering() should work.

ABPersonSortOrdering sortOrder = ABPersonGetSortOrdering();
if (sortOrder == kABPersonSortByFirstName) {
    // sort by firstName
}
else {
    // sort by lastName
}

if you look at Ericas code you'll see that it does not return the sort order. It actually returns the display format.

from ABContactsHelper.m:

// Sorting
+ (BOOL) firstNameSorting
{
    return (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst);
}

that's completely wrong.

Maybe you want to skip that 4 year old, unmaintained code and write something yourself. The AddressBook framework is not that hard to understand.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • That's it. Thank you. At the time I just chose it to get faster progress. Anyhow, there are existing forks github which provide iOS 5+ support... – crizztus Jul 14 '13 at 22:32
  • And how do I get notified if user changes the sort order again? – schystz Aug 05 '13 at 14:11
  • @schystz you won't get notified. You have to retrieve the sort ordering as described by Matthias Bauch – crizztus Nov 04 '13 at 09:04
  • 3
    @Matthias can you add please the code below for iOS 9 and higher in your answer ? thanks `[[CNContactsUserDefaults sharedDefaults] sortOrder]` – HamzaGhazouani Jun 20 '16 at 11:55