我怎样才能得到联系ID

How can I get the contact ID

我在从地址簿中获取联系人 ID 时遇到问题。

我的 .m 文件:

-(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。 每次我只得到 ID -1,这代表错误。

希望你能帮助我。

非常感谢, 克里斯蒂安

在iOS8中,不再在仅显示人员选择器时自动请求通讯录权限。 (这个想法是,单独的选择器并没有真正让应用程序访问您的联系人,因此不需要许可。当应用程序没有真正获取联系人详细信息时,它提供低摩擦 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);
    });
}

这里是我如何获取联系人 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:)