我需要返回特定组的所有联系人。所以一直在尝试做一些提取所有联系人的示例代码。首先需要找到电话簿中的所有组。当我打开电话簿时,我发现已经创建了 2 个组,因为我在Settings->Mail,Contacts,Calendars中设置了 2 个帐户。iCloud
和雅虎帐户。
以下是我为获取所有组名而编写的代码
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
// First time access has been granted, add the contact
// NSLog(@" given access..asked first time");
[self getContacts:addressBookRef];
});
}
else
[self getContacts:addressBookRef];
}
-(void) getContacts:(ABAddressBookRef) addressBookRef
{
NSString* groupName;
CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addressBookRef);
CFIndex count = ABAddressBookGetGroupCount(addressBookRef);
for (int i=0; i<count;i++) {
ABRecordRef record = CFArrayGetValueAtIndex(groups, i);
groupName = ( NSString *)ABRecordCopyCompositeName(record);
NSLog(@"Group %d : %@",i+1,groupName);
}
}
现在我应该得到 2 而我得到 0。谁能建议我在这里做错了什么?