Can CNContactPickerViewController be added as child view controller?

552 Views Asked by At

I want to add CNContactPickerViewController in a UITabBarController as a tab. I've achieved the same thing with ABPeoplePickerNavigationController but same approach didn't work for CNContactPickerViewController. Is there anything I've missed or is it impossible.

Here is what I tried:

Note: When I remove if #available(iOS 9, *) it also works in iOS 9.

class ContactsViewController: UIViewController, ABPeoplePickerNavigationControllerDelegate, CNContactPickerDelegate {

    var contactsController: UIViewController!
    override func awakeFromNib() {
        contactsController = getAddressBook()
        contactsController.view.frame = self.view.bounds

        self.addChildViewController(contactsController)
        contactsController.didMoveToParentViewController(self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.contactsController.view.frame = self.view.bounds
        self.view.addSubview(contactsController.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func getAddressBook() -> UIViewController {
        if #available(iOS 9, *) {
            let contactPicker = CNContactPickerViewController()
            contactPicker.delegate = self;
            contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]

            return contactPicker
        } else {
            let addressBookController = ABPeoplePickerNavigationController()
            addressBookController.peoplePickerDelegate = self

            return addressBookController
        }
    }
}
0

There are 0 best solutions below