user2375706
user2375706

Reputation: 805

Information about sorting in phone addres book iOS

I make controller which display all contacts avaiable in phone's address book and I have to make feature of sorting these contacts. Sorting should be the same as in phone's address book. For example if in phone's address book I have set sorting by last name in my app I should have the same.

Do you know how to get information about contacts sorting which is set in iphone settings?

Upvotes: 0

Views: 76

Answers (2)

xexe
xexe

Reputation: 346

Read this: https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/DirectInteraction.html#//apple_ref/doc/uid/TP40007744-CH6-SW1

There is this function call to sort the array of people:

CFArraySortValues(

    peopleMutable,

    CFRangeMake(0, CFArrayGetCount(peopleMutable)),

    (CFComparatorFunction) ABPersonComparePeopleByName,

    (void*) ABPersonGetSortOrdering()

);

I think that function

ABPersonGetSortOrdering()

is what you're looking for

Upvotes: 1

iOSDeveloper
iOSDeveloper

Reputation: 461

CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
                                                           CFArrayGetCount(people),
                                                           people);

CFArraySortValues(peopleMutable,
                  CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                  (CFComparatorFunction) ABPersonComparePeopleByName,
                  kABPersonSortByFirstName);

//or Use this sorting technique by the address books selected sorting technique

CFRelease(people);


for (CFIndex i = 0; i < CFArrayGetCount(peopleMutable); i++)
{
    ABRecordRef record = CFArrayGetValueAtIndex(peopleMutable, i);
    NSString *firstName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonFirstNameProperty));
    NSString *lastName = CFBridgingRelease(ABRecordCopyValue(record, kABPersonLastNameProperty));
    NSLog(@"person = %@, %@", lastName, firstName);
}

CFRelease(peopleMutable);

Upvotes: 0

Related Questions