3

On MAC platform, the ABPerson class has a parentGroups attribute, which tells us which groups the person belongs to. But On IOS platform, there is no such method or property..

How to find out the parent groups of a person? Do I needs to search through all groups one by one?

Thanks.

flypig
  • 1,260
  • 1
  • 18
  • 23

3 Answers3

6

another thread mentioned ABPersonCopyParentGroups -- but this is wrong -n.a. on ios.

there is no api call so doing this directly is needed

ABRecordRef personToFind = ....
ABRecordID id = ABRecordGetRecordID(personToFind);

NSArray *groups = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllGroups(bookRef);
for(ABGroupRef group in groups) {
    NSArray *members = (__bridge_transfer NSArray*)ABGroupCopyAllMembers(group);
    for(ABRecordRef member in members) {
        if(id == ABRecordGetRecordID(member){
            NSLog(@"found in group %@!", ABGroupCopyProperty(group, kABGroupName);
            break;
        }
    }            
}

*typed inline, no guarantees -- there are likely typos!

Shmidt
  • 16,436
  • 18
  • 88
  • 136
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
1

I had difficulties to use Daij-Djan answer, so I rewrote it:

CFArrayRef groupsRef = ABAddressBookCopyArrayOfAllGroups(myAddressBook);
if(groupsRef) {
    NSUInteger groupsCount = CFArrayGetCount(groupsRef);

    for(NSUInteger idx = 0; idx < groupsCount; ++idx) {
        ABRecordRef groupRef = CFArrayGetValueAtIndex(groupsRef, idx);
        ABRecordID groupID = ABRecordGetRecordID(groupRef);
        CFArrayRef membersRef = ABGroupCopyArrayOfAllMembers(groupRef);
        if(membersRef) {
            NSUInteger membersCount = CFArrayGetCount(membersRef);

            for(NSUInteger idx = 0; idx < membersCount; ++idx) {
                ABRecordRef memberRef = CFArrayGetValueAtIndex(membersRef, idx);
                ABRecordID memberID = ABRecordGetRecordID(memberRef);

                // your code
            }
            CFRelease(membersRef);
        }
    }
    CFRelease(groupsRef);
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
1

I scan all groups and people in them, and save person recordID and group recordID to a NSDictionary. I keep this NDDictionary till app gone, so just need to lookup this NSDictionary for person's group ID.

NSMutableDictionary *dic = [NSMutableDictionary dictionary];
CFArrayRef allGroup = ABAddressBookCopyArrayOfAllGroups( AddressBookManager.addressBook );

CFIndex nGroup = ABAddressBookGetGroupCount(AddressBookManager.addressBook);

NSMutableArray *array = [[NSMutableArray alloc] init];
for (CFIndex index = 0; index < nGroup; index ++) 
{
    ABRecordRef group = CFArrayGetValueAtIndex(allGroup, index);
    ABRecordID groupID = ABRecordGetRecordID(group);

    CFArrayRef allPeople = ABGroupCopyArrayOfAllMembers(group);

    CFIndex nPeople = CFArrayGetCount(allPeople);
    for( CFIndex personIndex = 0; personIndex < nPeople; personIndex++ )
    {
        ABRecordRef person = CFArrayGetValueAtIndex( allPeople, personIndex );
        ABRecordID personID = ABRecordGetRecordID(person);

        NSNumber *value = [NSNumber numberWithInt:groupID];
        NSString *key = [NSString stringWithFormat:@"%d", personID];
        [dic setValue:value forKey:key];
    }
    CFRelease(allPeople);
}
Jerry Juang
  • 147
  • 2
  • 2