CNContact获取和导入联系人到应用程序的示例

时间:2016-01-09 21:17:44

标签: ios xcode cncontact cncontactstore cncontactviewcontroller

我一直在使用ABPeople Picker来显示联系人,然后将所选联系人导入我的应用。

由于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);
    }
}

1 个答案:

答案 0 :(得分: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之类的电子邮件。等

用于选择多个联系人使用contactPicker:didSelectContacts: 代替上述代码中的contactPicker:didSelectContact: