2

I'm using the predicates defined here: How can I pick a contact phone number in iOS?.

contactsPicker.predicateForEnablingContact = NSPredicate(format:"phoneNumbers.@count > 0")
contactsPicker.predicateForSelectionOfContact = NSPredicate(format: "phoneNumbers.@count == 1")
contactsPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]

I have also defined the following delegate methods:

func contactPicker(_ picker: CNContactPickerViewController,
                   didSelect contactProperty: CNContactProperty) {
    print(contactProperty)
}

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
    print(contact)
}

The behaviour I expect is that the user gets to pick the phone number properties from multiple different contacts and when the user clicks done, I get a list of these properties and contacts.

Unfortunately, the code above exits the picker immediately when the user clicks done. This means the user cannot select multiple phone numbers and/or multiple contacts and can only select one before the picker exits.

How can I change it so that it selects multiple properties or contacts and submits on clicking on done?

Mahendra
  • 8,448
  • 3
  • 33
  • 56

2 Answers2

0

In order to select more than one contact property you need to define contactPicker:didSelectContactProperties: in your delegate instead of the methods you mentioned in your question.

Hope this helps!

Apple Documentation

0

If you want to implement multi-selection of contact then you need to implement CNContactPickerViewController's delegate i.e.CNContactPickerDelegate in your view controller, so it will automatically configure CNContactPickerViewController for multi-selection.

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;

If you want to allow user to select a contact only then you need to implement following methods...

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts;
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties;

Don't implement single selection and multi-selection delegate methods at same time.

NOTE: Implementing one of these methods will configure the picker for multi-selection.

Mahendra
  • 8,448
  • 3
  • 33
  • 56