John
John

Reputation: 548

CNContact Example of a Fetch and import of a Contact into App

I have been using ABPeople Picker to show Contacts and then import the selected contact into my app.

Need to migrate to CNContact as AB has become unreliable.

I have found some examples but they're all in Swift. Specifically, need ObjectiveC help with CNContactPickerViewController.

Current code looks like this:

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

Upvotes: 0

Views: 2917

Answers (1)

Nishanth Kumar
Nishanth Kumar

Reputation: 41

You can do so by the following code :

-(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);

}

The displayed property keys can be anything like CNContactEmailAddressesKey for email. etc

for selecting multiple contacts use contactPicker:didSelectContacts: instead of contactPicker:didSelectContact: in the above code

Upvotes: 1

Related Questions