2

I am using the CNContactViewController to enter information for a new client. But however I present the CNContactViewController I only get a "Cancel" button in the top bar... There is no Done button, or any other way to dismiss the controller.

How can I enable a Done button (or any other way for the user to accept the newly-entered contact information)?

I saw this question, but didn't help with the Done button... CNContactViewController does something strange with responder chain

Thanks

Community
  • 1
  • 1
MrMikeT
  • 53
  • 5
  • In your question I can't find what you already tried yourself (code) or what error you're getting. please read How to ask stackoverflow.com/help/how-to-ask And How to create a Minimal, Complete, and Verifiable example stackoverflow.com/help/mcve. – davejal Nov 22 '15 at 00:59
  • CNContactViewController can be used several different ways. You are not showing how _you_ are using it, so the question is meaningless. – matt Nov 22 '15 at 01:24
  • After much trial and error I finally found that I need to get access to the device Contacts using the requestAccessForEntityType: method before trying to use the CNContactViewController. Once I have authorization the Done button appears. Thanks for the tips on how to ask better questions; I'll add code examples next time... – MrMikeT Nov 22 '15 at 04:01

1 Answers1

0

You can subclass CNContactViewController to add a done button to the navigationItem.

Something like this:

import ContactsUI

class ContactDetailsViewController: CNContactViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        updateNavigationItem()
    }

    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        updateNavigationItem()
    }

    private func updateNavigationItem() {
        if !isEditing {
            // add a 0.5 seconds delay to add button after the editing animation finishes
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                self.navigationItem.leftBarButtonItem = .init(
                    barButtonSystemItem: .done,
                    target: self,
                    action: #selector(self.doneTapped)
                )
            }
        }
    }

    @objc private func doneTapped() {
        dismiss(animated: true)
    }
}

In order to make use of this class, make sure that ContactDetailsViewController is embedded inside a UINavigationController.

Although I'm so late to answer, but I hope this helps you or someone else.

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60