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

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

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

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

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

谢谢。

最佳答案

使用ABRecordCopyCompositeName()函数。

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);
    });
}

关于iphone - 获取联系人中的所有组名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18169617/

10-10 22:43