0

我一直在使用ABPeoplePicker 显示联系人,然后将选定的联系人导入我的应用程序。

需要迁移到CNContact因为AB已经变得不可靠。

我找到了一些例子,但它们都在 Swift 中。具体来说,需要 ObjectiveC 的帮助CNContactPickerViewController

当前代码如下所示:

-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person 
{
    CFTypeRef generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);

    if (generalCFObject) {
        self.first = (__bridge_transfer NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

        NSLog (@"First Name %@",first);
    }
}
4

1 回答 1

1

您可以通过以下代码执行此操作:

-(void)selectContactData {

    CNContactPickerViewController * picker = [[CNContactPickerViewController alloc] init];

    picker.delegate = self;
    picker.displayedPropertyKeys = (NSArray *)CNContactGivenNameKey;

    [self presentViewController:picker animated:YES completion:nil];

}

-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {

    [self dismissViewControllerAnimated:YES completion:nil];
    NSString *test = contact.givenName;
    NSLog(@"%@",test);

}

显示的属性键可以是任何类似于CNContactEmailAddressesKey电子邮件的键。ETC

用于选择多个联系人contactPicker:didSelectContacts: ,而不是contactPicker:didSelectContact:在上面的代码中

于 2016-02-26T07:04:48.343 回答