4

我需要返回特定组的所有联系人。所以一直在尝试做一些提取所有联系人的示例代码。首先需要找到电话簿中的所有组。当我打开电话簿时,我发现已经创建了 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。谁能建议我在这里做错了什么?

4

1 回答 1

1

ABAddressBookGetAuthorizationStatus() 是否有可能返回 kABAuthorizationStatusRestricted 或 kABAuthorizationStatusDenied?根据您提供的代码片段,返回值 kABAuthorizationStatusRestricted 或 kABAuthorizationStatusDenied 将使您进入 getContacts 方法,而无需适当的通讯簿授权。

于 2013-05-29T15:38:32.587 回答