CNContactViewController Cancel Button Not Working

Viewed 4183

I'm trying to use the built-in new contact UI and am getting unexpected behavior with the cancel button. The code below works and calls up the new contact screen but the cancel button will only clear the screen entries not cancel out of the new contact screen. In the built in contacts app hitting cancel returns to the contact list screen. I would like the cancel button to close out the window.

@IBAction func newTwo(sender: AnyObject) {
    AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
        if accessGranted {
            let npvc = CNContactViewController(forNewContact: nil)
            npvc.delegate = self
            self.navigationController?.pushViewController(npvc, animated: true)
         }
    }

}
3 Answers

It worked for me using the following code:

Swift 3

func contactViewController(_ vc: CNContactViewController, didCompleteWith con: CNContact?) {
    vc.dismiss(animated: true)
}

Also I changed the way I was calling the controller:

Instead of:

self.navigationController?.pushViewController(contactViewController, animated: true)

the solution was:

self.present(UINavigationController(rootViewController: contactViewController), animated:true)

I found the solution using the example code Programming-iOS-Book-Examples written by Matt Neuburg:

Related