ABAddressBookGetPersonWithRecordID返回NULL

时间:2015-08-29 14:37:42

标签: ios null contacts

在myapp中,我从联系人中选择电话号码。 我需要存储记录ID,然后检索recordref。 为了测试逻辑,我尝试了以下示例 在下面的代表中,我记录了该人的记录ID。 然后尝试检索Recordref。 记录器(gref)为空 请让我知道我做错了什么。 我已经在OS 6上使用os 8.2进行了测试

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{

     ABRecordID sid = ABRecordGetRecordID(person);

     ABAddressBookRef addressBook = ABAddressBookCreate();

     ABRecordRef gref = ABAddressBookGetPersonWithRecordID(addressBook, sid);

     NSLog(@“org record ref %@",person);

     NSLog(@“new record ref %@",gref);

}

1 个答案:

答案 0 :(得分:0)

在使用地址簿之前,我们需要获得访问地址簿的权限。 示例

CFErrorRef myError = NULL;
ABAddressBookRef myAddressBook = ABAddressBookCreateWithOptions(NULL, &myError);

__block ABRecordRef gref;

ABAddressBookRequestAccessWithCompletion(myAddressBook,
                                         ^(bool granted, CFErrorRef error) {
                    if (granted) {

                        gref = ABAddressBookGetPersonWithRecordID(myAddressBook, sid);
                        NSString *contactName = CFBridgingRelease(ABRecordCopyCompositeName(gref));

                        NSLog(@"after record ref %@",gref);
                        NSLog(@"human name %@",contactName);

                    } else {

                        // Handle the case of being denied access and/or the error.

                    }

                                             CFRelease(myAddressBook);

            });