问题描述
- 获取通讯录中的所有群组名字
-
怎么样获取保存在通讯录中的所有群组名字?相关代码:NSMutableArray *list = [NSMutableArray array]; ABAddressBookRef myAddressBook = ABAddressBookCreate(); CFArrayRef allSources = ABAddressBookCopyArrayOfAllGroups(myAddressBook); list = [NSMutableArray arrayWithArray: (__bridge NSArray*) allSources]; NSLog(@"GROUPS %@",[list objectAtIndex:0]);
谢谢
解决方案
使用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);
});
}
时间: 2024-12-29 20:18:38