Ios 如何获取联系人ID
我无法从通讯簿中获取联系人ID 我的.m文件:Ios 如何获取联系人ID,ios,ios8,ios8.1,addressbookui,Ios,Ios8,Ios8.1,Addressbookui,我无法从通讯簿中获取联系人ID 我的.m文件: -(IBAction)test:(id)sender { ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init]; picker.peoplePickerDelegate = self; [self presentViewController:picker animated:YES compl
-(IBAction)test:(id)sender
{
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
ABRecordID recordID = ABRecordGetRecordID(person);
label.text = [NSString stringWithFormat:@"%d", recordID];
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
我将AddressBook和AddressBookUI导入到.h文件中。
一切正常,但我拿错了身份证。
每次我只得到ID-1,这代表错误
希望你能帮助我
非常感谢,
Christian在iOS 8中,它不再在仅显示人员选取器时自动请求地址簿的权限。(其想法是,仅选取者并不能真正让应用程序访问您的联系人,因此不需要任何权限。当应用程序无法真正获取联系人详细信息时,它提供了低摩擦的用户界面。) 但是,在您的情况下,您实际上是在尝试通过编程检索有关联系人的信息。为此,您确实需要许可。因此,在介绍选取者之前,您可能希望获得地址簿的许可:
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);
});
}
下面是如何获取联系人ID的解决方案:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
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);
});
}
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
ABRecordID recordID = ABRecordGetRecordID(person);
NSLog([NSString stringWithFormat:@"%@"], recordID);
}
感谢帮助我的Rob:)顺便说一句,请注意授权完成块不会在主线程上运行,因此您在该块中执行的任何UI操作都必须发送回主队列。非常感谢。现在它对我有效:)通过我的解决方案和Rob的工作,您可以获得联系人的ID;)这对我也有用。通过上面的更改,ABRecordGetRecordID不再像以前一样返回-1(kABRecordInvalidID)。文档称ABRecordGetRecordID在“记录尚未保存到数据库时”返回kABRecordInvalidID。对此,我当然要补充:“如果应用程序尚未使用ABAddressBookRequestAccessWithCompletion请求访问通讯簿,也会返回kABRecordInvalidID(…).好的,我改了,用正确的答案回答了我的问题;)谢谢你的时间和帮助。:)