How to use CNContactViewController just for creating new contacts? (Swift)

1.7k Views Asked by At

I have an app that needs to add contacts. For that I want use the CNContactViewController(forNewContact: nil) from the ContactsUI framework.

My approach to achieve this is the following

class myClass: UITableViewController, CNContactViewControllerDelegate {
    // ...
    @objc fileprivate func addTapped() {
        print("add tapped")

        let createContactcontroller = CNContactViewController(forNewContact: nil)
        controller.delegate = self

        navigationController?.pushViewController(controller, animated: true)
    }

    func contactViewController(_ viewController: CNContactViewController, didCompleteWith contact: CNContact?) {
        // Service.shared.addContact(contact: contact)
    }

}

But there are a few problems that I can't solve:

  1. The animation looks laggy.
  2. When the user is done with creating the contact and clicks done, than the CNContactViewController for profile (like in contacts app) is shown, instead of just returning to tableViewController. How can I modify this?

enter image description here

  1. I get following errors:

2019-11-16 10:29:48.480636+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc76481a420, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES. 2019-11-16 10:29:48.482425+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc76481d020, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES. 2019-11-16 10:29:48.484897+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc76481d5c0, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES. 2019-11-16 10:29:48.488071+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc76481d9f0, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES. 2019-11-16 10:29:48.489883+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc7626228e0, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES. 2019-11-16 10:29:48.491904+0100 Flashback[11688:541742] [Snapshotting] Snapshotting a view (0x7fc7626102d0, _UIButtonBarStackView) that has not been rendered at least once requires afterScreenUpdates:YES.

1

There are 1 best solutions below

1
On BEST ANSWER

You are using this view controller wrong.

You must not push it onto an existing view controller. Instead you instantiate a UINavigationController with the CNContactViewController as its root view controller, and present the navigation controller as a presented view controller. You must then dismiss the presented navigation controller yourself, in your implementation of contactViewController(_:didCompleteWith:).

Also watch out for this bug, which makes this view controller largely unusable:

Keyboard overlaying action sheet in iOS 13.1 on CNContactViewController