我如何获得联系人ID

时间:2015-02-26 19:01:01

标签: ios ios8 ios8.1 addressbookui

我从地址簿中获取联系人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,这代表错误。

希望你能帮助我。

非常感谢, 基督教

2 个答案:

答案 0 :(得分:1)

在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);
    });
}

答案 1 :(得分:0)

这是我的解决方案,如何获取联系人的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帮我解决问题:)