-1

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.

Eduard
  • 61
  • 7
  • That doesn't work either: https://imgur.com/a/STuopuD – Eduard Nov 16 '19 at 10:13
  • for 1.The animation looks laggy, set background color before you push. `createContactcontroller.view.backgroundColor = .white` – Ratnesh Jain Nov 16 '19 at 10:18
  • 1
    @RatneshJain that worked excellent. Do you have a reference for me where I can look up why this happened? Current state: https://imgur.com/a/L3cU4Fv – Eduard Nov 16 '19 at 10:20
  • 1
    UIView has backgroundColor property which is optional. ```@NSCopying open var backgroundColor: UIColor?``` So the default value is `nil`. So iOS renders it as black color or no color. When you initialize any UIView or UIViewController from Xib or Storyboard it sets views background color in xml. So you do not need to set background color in code. `````` (You can open your storyboard as code and search for backgroundColor property). – Ratnesh Jain Nov 16 '19 at 10:35
  • @matt I did the exact same thing as in your book [Programming iOS 13](https://www.oreilly.com/library/view/programming-ios-13/9781492074601/) but I'm still having the same issues with the errors. – Eduard Nov 16 '19 at 11:11
  • 1
    They aren’t your errors. Ignore them. And what my book says is that this thing is disastrously buggy. – matt Nov 16 '19 at 14:53
  • That's a helpful answer. Thank you. I thought I did something wrong. – Eduard Nov 16 '19 at 21:20

1 Answers1

1

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

matt
  • 515,959
  • 87
  • 875
  • 1,141