Search code examples
swiftcncontactpicker

CNContactPickerViewController when contact has multiple mails


I'm using a CNContactPickerViewController to let the user select the email of one of his/her contacts

let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self
contactPicker.predicateForSelectionOfContact = NSPredicate(format: "emailAddresses.@count > 0")
contactPicker.displayedPropertyKeys = [CNContactNicknameKey, CNContactEmailAddressesKey]

When the contact has only one mail, everything goes well

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

    if let _mail = contact.emailAddresses.first?.value as String? {
        self.personWasSelected(with: _mail)
    }
}

But sometimes one of the contacts has more than one mail, how can I let the user select on one of those?


Solution

  • Ok, found the solution by trial-error...

    I was implementing both:

    func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
        if let _mail = contactProperty.value as? String {
            self.personWasSelected(with: _mail)
        }
    }
    
    func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
    
        if let _mail = contact.emailAddresses.first?.value as String? {
            self.personWasSelected(with: _mail)
        }
    }
    

    But only the first one should have been implemented