1

如何获取保存在联系人中的所有组名?以下是我的代码供您参考:

NSMutableArray *list = [NSMutableArray array];
ABAddressBookRef myAddressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllGroups(myAddressBook);

list = [NSMutableArray arrayWithArray: (__bridge NSArray*) allSources];

NSLog(@"GROUPS %@",[list objectAtIndex:0]);

NSLog 返回 a 但我需要获取组名本身。

谢谢。

4

2 回答 2

2

Use the ABRecordCopyCompositeName() function.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != NULL) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            CFArrayRef allGroups = ABAddressBookCopyArrayOfAllGroups(addressBook);
            if (allGroups != NULL) {
                NSMutableArray *names = [NSMutableArray array];
                for (int i = 0; i < CFArrayGetCount(allGroups); i++) {
                    ABRecordRef group = CFArrayGetValueAtIndex(allGroups, i);
                    CFStringRef name = ABRecordCopyCompositeName(group);
                    [names addObject:(__bridge NSString *)name];
                    CFRelease(name);
                }
                NSLog(@"names = %@", names);
                CFRelease(allGroups);
            }
        }
        CFRelease(addressBook);
    });
}
于 2013-08-11T07:29:05.863 回答
0

这对我有用:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);

NSArray *allGroups = CFBridgingRelease(ABAddressBookCopyArrayOfAllGroups(addressBook));
NSInteger numberOfGroups = [allGroups count];

for (NSInteger i = 0; i < numberOfGroups; i++) {
    ABRecordRef group = (__bridge ABRecordRef)allGroups[i];

    NSString *groupName = CFBridgingRelease(ABRecordCopyCompositeName(group));
    NSLog(@"group = %@",groupName);

    NSLog(@"=============================================");
}
于 2015-07-17T00:48:58.390 回答