I have this code below that I managed to get the names & phone numbers listed out from the address book, but how do I sort it by the first name?
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
abContactArray = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef); // get address book contact array
NSInteger totalContacts =[abContactArray count];
for(NSUInteger loop= 0 ; loop < totalContacts; loop++)
{
ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record
if(ABRecordGetRecordType(record) == kABPersonType) // this check execute if it is person group
{
//ABRecordID recordId = ABRecordGetRecordID(record); // get record id from address book record
//NSString *recordIdString = [NSString stringWithFormat:@"%d",recordId]; // get record id string from record id
//NSLog(@"Record: %@", recordIdString);
NSString *firstNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonFirstNameProperty); // fetch contact first name from address book
NSString *lastNameString = (__bridge NSString*)ABRecordCopyValue(record,kABPersonLastNameProperty); // fetch contact last name from address book
//NSString *contactEmail = (__bridge NSString*)ABRecordCopyValue(record,kABPersonEmailProperty); // fetch contact last name from address book
NSString * fullName = [NSString stringWithFormat:@"%@ %@", firstNameString, lastNameString];
[name addObject: fullName];
ABMultiValueRef phoneNumberMultiValue = ABRecordCopyValue(record, kABPersonPhoneProperty);
NSUInteger phoneNumberIndex;
for (phoneNumberIndex = 0; phoneNumberIndex < ABMultiValueGetCount(phoneNumberMultiValue); phoneNumberIndex++) {
CFStringRef labelStingRef = ABMultiValueCopyLabelAtIndex (phoneNumberMultiValue, phoneNumberIndex);
//NSString *phoneLabelLocalized = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(labelStingRef);
phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneNumberMultiValue, phoneNumberIndex);
CFRelease(labelStingRef);
//NSLog(@"Name: %@ %@: %@ | %@", firstNameString, lastNameString, phoneNumber, emailAddresses);
}
[phone addObject: phoneNumber];
}
}
I tried putting in these codes:
ABRecordRef record = (__bridge ABRecordRef)[abContactArray objectAtIndex:loop]; // get address book record
//ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
kCFAllocatorDefault,
CFArrayGetCount(people),
people
);
CFArraySortValues(
peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction) ABPersonComparePeopleByName,
(void*) ABPersonGetSortOrdering()
);
//NSMutableArray *data = [(__bridge NSArray *) peopleMutable mutableCopy];
NSMutableArray* data = [NSMutableArray arrayWithArray: (__bridge NSArray*) peopleMutable];
NSLog(@"sort: %@", data);
But the nslog gave me these output:
sort: (
"<CPRecord: 0xaa5d250 ABPerson>",
"<CPRecord: 0xaa6e050 ABPerson>",
"<CPRecord: 0xaa3d7d0 ABPerson>",
"<CPRecord: 0xaa515d0 ABPerson>",
"<CPRecord: 0xaa43b90 ABPerson>",
"<CPRecord: 0xaa6b780 ABPerson>"
)
You can sort the entries by name using:
Or you could use this technique:
The former seems cleaner, but just another option, in case you want to avoid
CFReleasein an ARC world.By the way, use
ABAddressBookRequestAccessWithCompletionin iOS 6 to check to make sure you have permission to access the address book (with a conditional check to make sure it still works with earlier versions of iOS). And even if you’re on earlier versions of iOS, you should be asking the user for there permission manually.