Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CNContactViewController Cancel Button Not Working

Tags:

ios

swift

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)
         }
    }

}
like image 995
Steve M Avatar asked Mar 17 '16 22:03

Steve M


2 Answers

did you implement CNContactViewControllerDelegate methods? Here's a link to documentation

for example:

 func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {

    self.dismissViewControllerAnimated(true, completion: nil)
}
like image 111
Tomasz Rejdych Avatar answered Sep 28 '22 13:09

Tomasz Rejdych


Better way to do the dismissing would be to check if the contact is nil and then dismiss it. The dismiss doesn't work if you've pushed the view controller from a navigation controller. You might have to do the following:

func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
  if let contactCreated =  contact
  {

  }
  else
  {
    _ = self.navigationController?.popViewController(animated: true)
  }
}
like image 41
Koushik Ravikumar Avatar answered Sep 28 '22 12:09

Koushik Ravikumar