2 yue1liang1chuan yue1liang1chuan 于 2013.08.12 10:46 提问

获取通讯录中的所有群组名字

怎么样获取保存在通讯录中的所有群组名字?相关代码:

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

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

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

谢谢

1个回答

Chengzi_963
Chengzi_963   2013.08.12 15:16
已采纳

使用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);
    });
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!