-1

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.

2 Answers2

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
  • get all number and check condition emailid has or not – Kirit Modi Mar 31 '14 at 09:02
  • 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
  • If contact number has no EmailId then it not require You' – Kirit Modi Mar 31 '14 at 14:02
  • 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