I am developing an app, in which I want to fetch all the contacts from addressbook and show in my app.I have get all the data but problem is,l this data is not in ascending order alphabetically, also phone numbers, email id should be arrange in proper sequence as names.I have all the data in different different arrays.Please give some idea for correct this.
Asked
Active
Viewed 866 times
-1
-
http://stackoverflow.com/questions/3747844/get-a-list-of-all-contacts-on-ios – Tarek Hallak Mar 31 '14 at 06:43
-
did you fetched the contacts from address book successfully? – Himanshu Joshi Mar 31 '14 at 06:44
-
@HimanshuJoshi Yes i ahve fetched, but problem is I cant sort all data from different arrays – user255906 Mar 31 '14 at 08:46
-
Did you try with my answer? – Himanshu Joshi Mar 31 '14 at 08:57
2 Answers
4
First of all import <AddressBook/AddressBook.h>
in Your .m File
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
[allEmails addObject:email];
}
CFRelease(emails);
}
NSLog(@"All Detils:%@",allEmails);
CFRelease(addressBook);
CFRelease(people);
You Can get all emailAdress as above code.You wnat to First name then change.
ABMultiValueRef Name = ABRecordCopyValue(person, kABPersonFirstNameProperty);
Contact number:
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
Only You changes person property which you want.
// Property keys
AB_EXTERN const ABPropertyID kABPersonFirstNameProperty; // First name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNameProperty; // Last name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNameProperty; // Middle name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonPrefixProperty; // Prefix ("Sir" "Duke" "General") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonSuffixProperty; // Suffix ("Jr." "Sr." "III") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonNicknameProperty; // Nickname - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonFirstNamePhoneticProperty; // First name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNamePhoneticProperty; // Last name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNamePhoneticProperty; // Middle name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonOrganizationProperty; // Company name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonJobTitleProperty; // Job Title - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonDepartmentProperty; // Department name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonEmailProperty; // Email(s) - kABMultiStringPropertyType
AB_EXTERN const ABPropertyID kABPersonBirthdayProperty; // Birthday associated with this person - kABDateTimePropertyType
AB_EXTERN const ABPropertyID kABPersonNoteProperty; // Note - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonCreationDateProperty; // Creation Date (when first saved)
AB_EXTERN const ABPropertyID kABPersonModificationDateProperty; // Last saved date
After sort that array in alphabetical order
NSArray *EmailArray = [allEmails sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Kirit Modi
- 23,155
- 15
- 89
- 112
-
I think it should be good, but problem is that in starting you have explained for email, but it will give only the contacts which have email. for ex. I have 20 contacts in my addresbook, I want to fetch all 20, even they have phone number and email or not. – user255906 Mar 31 '14 at 08:50
-
-
but using this it will give that result which contains emil id,but I want all the contacts which contains phone number & email id exist or not. – user255906 Mar 31 '14 at 14:00
-
-
but user wants all contacts wether it contains phone number and email id or not, he wants to show all contacts, and user can update their contact details run time – user255906 Mar 31 '14 at 14:11
0
If you have successfully find out the contacts and stored them in array, then you can sort it alphabetically using localizedCaseInsensitiveCompare:
NSArray *sortedArray = [YOUR_ARRAY sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Just go through Apple Docs, you can find everything there.

Himanshu Joshi
- 3,391
- 1
- 19
- 32