1

I'm working on a custom "contacts" implementation for Mac and I'm finding that I can't get the equivalent behavior of ABSource from iOS. For example, in the Mac contacts app, I can see my iCloud source and it's groups underneath it. When I query ABAddressBook for it's groups, the only thing I get back is the "untitled group" (see below). So ultimately, what I'm asking is how can I get the source (iCloud) from my ABGroup group (untitled group) so that I can build a similar UI. It seems that this capability is missing in Mac - I'm hoping I'm just overlooking it.

https://i.stack.imgur.com/RHIll.png

cmilack
  • 11
  • 4

1 Answers1

0

What you're getting is correct.

The only group that you have, is the "untitled group". "All iCloud" is not an ABGroup, but instead all the contacts that do not have a group set.

If you want to replicate the same UI, then you can do the following:

// get your addressbook reference first

// Create mutable dictionary for holding
NSMutableDictionary *sourceDisplayDictionary = [NSMutableDictionary dictionary];
NSArray *sources = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllSources(addressbook));

// Cannot use "ABRecordRef" in fast enumeration
for (id source in sources) {
    ABRecordRef sourceRef = (__bridge ABRecordRef)(source);
    NSString *sourceName = (__bridge NSString *)(ABRecordCopyValue(sourceRef, kABSourceNameProperty));

    // The "All" groups aren't real groups, they're placeholders for ABPersons without a group
    NSString *beginningGroup = [@"All " stringByAppendingString:sourceName];
    NSMutableArray *groupNames = [NSMutableArray arrayWithObject:beginningGroup];
    NSArray *groups = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllGroupsInSource(addressbook, sourceRef));

    // Again, iterate over the array of groups _inside_ the source to build the subarray array
    for (id group in groups) {
        ABRecordRef groupRef = (__bridge ABRecordRef)(group);
        NSString *groupName = (__bridge NSString *)(ABRecordCopyValue(groupRef, kABGroupNameProperty));
        [groupNames addObject:groupName];
    }

    // Add the source name to the dictionary
    sourceDisplayDictionary[sourceName] = [groupNames copy];
}

NSDictionary *sourceDictionary = [sourceDisplayDictionary copy];

/*
 {
   "iCloud":
   [
     "All iCloud",
     "untitled group"
   ]
 }
 */
Tim
  • 597
  • 1
  • 5
  • 15