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

2

6

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.

Airdry answered 27/9, 2012 at 11:26 Comment(0)
A
10
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.

Audiphone answered 27/9, 2012 at 11:47 Comment(0)
B
2

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
Burrill answered 27/9, 2012 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.