CNContactPickerViewController - Cannot make any selection

1k Views Asked by At

I am using CNContactPickerViewControllerto allow the user to see their contact list and select a contact. However, although I can show them their contact list, there is no ability to select any contacts. I am only looking for them to select a single contact. In the list of names there is no selector to tap and if you tap a person, you get their contact card. There is nothing on that screen that allows them to pick the contact either. So net, I can show them their contacts but they cannot select anyone. I have tried this on both the simulator and on an actual device.

Below is the class I created to handle what should be a simple process:

import Foundation
import ContactsUI

class ContactsController: NSObject, CNContactPickerDelegate {

    private var parentController:UIViewController!

    init(parentController:UIViewController) {
        super.init()
        self.parentController = parentController
        openContactList()
    }

    private func openContactList() {
        let picker = CNContactPickerViewController()
        picker.delegate = self
        picker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
        self.parentController.present(picker, animated: true)
    }

    func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        print("\(contact.givenName) \(contact.familyName): \(contact.phoneNumbers.first)")
    }
}

As an update, I implemented:

func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
    print("Canceled")
}

This method is never called. My assumption is that something is wrong with the delegate.Incidentally, I did move the instantiation of the below to the class level in case it was going out of scope. This did not help:

let picker = CNContactPickerViewController()

1

There are 1 best solutions below

0
On

The issue was that my delegate was going out of scope. The problem was not in the class I posted in my OP. I was calling this class from an IBAction in a UITableViewCell which was going out of scope thus this class was lost. Holding my ContactsController variable at a class level from the calling class, resolved this issue.