ABAddressBookGetGroupCount没有给出正确的计数

时间:2013-03-07 06:59:10

标签: iphone ios contacts addressbook abaddressbook

我需要返回特定群组的所有联系人。所以一直试图做一些提取所有联系人的示例代码。首先需要找到电话簿中的所有组。当我打开电话簿时,我发现已经创建了2个组,因为我在设置 - >邮件,通讯录,日历viz 中设置了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.任何人都可以建议我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

ABAddressBookGetAuthorizationStatus()是否可能返回kABAuthorizationStatusRestricted或kABAuthorizationStatusDenied?根据您提供的代码段,kABAuthorizationStatusRestricted或kABAuthorizationStatusDenied的返回值会将您放入getContacts方法,而无需正确的通讯簿授权。