Try this it works for iOS 6 as well as iOS 5.0 or older:
First add the following frameworks in Link Binary With Libraries
- AddressBookUI.framework
- AddressBook.framework
Them Import
#import <AddressBook/ABAddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
Then use the following code
ABAddressBookRef addressBook = ABAddressBookCreate();
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
}
else { // We are on iOS 5 or Older
accessGranted = YES;
[self getContactsWithAddressBook:addressBook];
}
if (accessGranted) {
[self getContactsWithAddressBook:addressBook];
}
// Get the contacts.
- (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger index = 0;
NSMutableArray *firstNames = [[NSMutableArray alloc] init];
for(index = 0; index <= ([arrayOfPeople count] - 1); index++){
ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);
// If first name is empty then don't add to the array
if (![currentFirstName length] == 0) {
[firstNames addObject: currentFirstName];
}
}
//OPTIONAL: Use the following line to sort the first names alphabetically
NSArray *sortedFirstNames = [firstNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"Total Count = %d \n Sorted By Name = %@", [sortedFirstNames count], sortedFirstNames);
}
I tested this and it works.