我正在为Mac使用自定义的“联系人”实现,发现无法从iOS获得ABSource的等效行为。例如,在Mac通讯录应用程序中,我可以看到我的iCloud源及其下面的组。当我查询ABAddressBook的组时,我唯一得到的是“无标题的组”(请参见下文)。因此,最终,我要问的是如何从ABGroup组(无标题组)获取源(iCloud),以便构建类似的UI。 Mac似乎缺少此功能-我希望我只是忽略了它。

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

最佳答案

您得到的是正确的。

您拥有的唯一组是“无标题的组”。 “所有iCloud”不是ABGroup,而是所有未设置组的联系人。

如果要复制相同的UI,则可以执行以下操作:

// 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"
   ]
 }
 */

关于macos - OS X的ABAddressBook/ABSource,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21338134/

10-16 00:50