我在从通讯簿中获取联系人 ID 时遇到问题。
我的 .m 文件:
-(IBAction)testid)sender
{
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)peoplePickerNavigationControllerABPeoplePickerNavigationController *)peoplePicker didSelectPersonABRecordRef)person
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
ABRecordID recordID = ABRecordGetRecordID(person);
label.text = [NSString stringWithFormat"%d", recordID];
}
-(void)peoplePickerNavigationControllerDidCancelABPeoplePickerNavigationController *)peoplePicker
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
我将 AddressBook 和 AddressBookUI 导入到 .h 文件中。
一切正常,但我得到了错误的 ID。
每次我只得到 ID -1 代表错误。
希望你能帮助我。
非常感谢,
基督徒
Best Answer-推荐答案 strong>
在 iOS 8 中,它不再在仅显示人员选择器时自动请求地址簿的权限。 (这个想法是,选择器本身并没有真正授予应用程序访问您的联系人的权限,因此不需要权限。当应用程序没有真正获取联系人详细信息时,它提供了低摩擦的 UI。)
但是,就您而言,您确实是在尝试以编程方式检索有关联系人的信息。为此,您确实需要许可。因此,在展示选择器之前,您可能需要请求对地址簿的许可:
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if ((status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted)) {
NSLog(@"User previously denied permission; show error message that they must go to settings and give this app permission");
return;
} else if (status == kABAuthorizationStatusNotDetermined) {
CFErrorRef error;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook) {
NSLog(@"unable to open address book: %@", CFBridgingRelease(error));
return;
}
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"OK");
} else {
NSLog(@"Not ok");
}
CFRelease(addressBook);
});
}
关于ios - 如何获取联系人 ID,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28750625/
|