Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get all contacts from particular Group in Addressbook?

Hi I have several Groups in my iPhone addressbook which contains several contacts. For Example:

iPhone addressbook, Group1, Group2 etc.

Each group contains contacts information like First Name,Last name,Email ,Phone number. Now by selecting any group I should get all details of added contacts in it. Can anybody please guide me how can I get all contacts details from particular group?

Please need some guidelines.

like image 387
Ankit Vyas Avatar asked Sep 27 '12 11:09

Ankit Vyas


Video Answer


2 Answers

CFErrorRef error = NULL;
ABAddressBookRef addrBook = ABAddressBookCreate();
CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addrBook);
CFIndex numGroups = CFArrayGetCount(groups);
for(CFIndex idx=0; idx<numGroups; ++idx) {
    ABRecordRef groupItem = CFArrayGetValueAtIndex(groups, idx);

    CFArrayRef members = ABGroupCopyArrayOfAllMembers(groupRef);
    if(members) {
        NSUInteger count = CFArrayGetCount(members);
        for(NSUInteger idx=0; idx<count; ++idx) {
            ABRecordRef person = CFArrayGetValueAtIndex(members, idx);

            // your code
        }
        CFRelease(members);
    }
}

CFRelease(groups);
CFRelease(addrBook);

This code is not guaranteed leak-proof so double check it. Its more or less correct.

like image 97
David H Avatar answered Oct 18 '22 22:10

David H


Everything is explained in the documentation, so please tell use what you don't understand in it. What did you try? What did you get, which errors did you have?

If you want to work with contacts, in addition to the very complete Address Book Programming Guide, you have of course the Address Book Framework Reference and especially the ABGroup Reference Documentation to work with groups. And the latter contains an explicitly a method to get all members of a group. So you should have everything you need here.

CFArrayRef cfmembers = ABGroupCopyArrayOfAllMembers(group);
NSArray* members = (NSArray*)cfmembers; // working with NSArray is usually easier that CFArrays so I like using toll-free bridging
for(ABRecordRef person in members)
{
  // ... your code ...
}
CFBridgingRelease(cfmembers); // release memory when done, following the usual memory mgmt rules
like image 31
AliSoftware Avatar answered Oct 18 '22 22:10

AliSoftware