Unable to dismiss CNContactViewController

喜欢而已 提交于 2019-12-04 03:17:22

The view controllers must be embedded in UINavigationController and you should push or show view controller:

navigationController?.pushViewController(contactPicker, animated: true)

instead of presenting view controller

You have to embed contactViewController to UINavigationController and Implement Delegate Methods.

let createNewActionHandler = {(action: UIAlertAction) -> Void in
    let newContact = CNMutableContact()

    let contactPicker = CNContactViewController(forNewContact: newContact)
    contactPicker.delegate = self
    let navigation = UINavigationController(rootViewController: contactPicker)
    self.presentViewController(navigation, animated: true, completion: nil)

}


//MARK: - Delegate

  func contactViewController(viewController: CNContactViewController, didCompleteWithContact contact: CNContact?) {
      viewController.dismissViewControllerAnimated(true, completion: nil)
  }

  func contactViewController(viewController: CNContactViewController, shouldPerformDefaultActionForContactProperty property: CNContactProperty) -> Bool {
      return true
  }

Since you are presenting contactPicker viewcontroller on top of current active controller, you will not have access to navigationbar as the view is presented fully,if you want to have button's as in Apple contact app you need to embed your presenting viewcontroller inside UINavigationController, and add left and right bar button items.

Refer the following apple sample which demonstrates the same. https://developer.apple.com/library/ios/samplecode/iPhoneCoreDataRecipes/Listings/Classes_RecipeAddViewController_m.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!