CNContactViewController blank when assigning parentGroup

227 Views Asked by At

Given the following:

let contactStore = CNContactStore()
let groupId = '<a valid group ID>'
let keysToFetch = [ // removed for brevity ]
var group: CNGroup? = nil

do {
  let predicate = CNGroup.predicateForGroups(withIdentifiers: [groupId])
  group = try contactStore.groups(matching: predicate).first
} catch let error as NSError {
  callback([error.localizedDescription, false])
}

let contactController = CNContactViewController(forNewContact: nil)
contactController.parentGroup = group
contactController.contactStore = contactStore
contactController.delegate = self
contactController.allowsEditing = true
contactController.allowsActions = true
contactController.displayedPropertyKeys = keysToFetch

When the controller is displayed via:

self.contactController = contactController

let navigationController = UINavigationController(rootViewController: contactController)
self.navigationController = navigationController

var currentViewController = UIApplication.shared.keyWindow?.rootViewController
while((currentViewController?.presentedViewController) != nil) {
  currentViewController = currentViewController?.presentedViewController
}

DispatchQueue.main.async(execute: {
  currentViewController?.present(navigationController, animated: true) {
    contactController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.cancel, target: self, action: #selector(self.cancelButtonPressed))
  }
})

The presented CNContactViewController() is blank:

enter image description here

What am I doing wrong? The documentation is very sparse around parentGroup.

1

There are 1 best solutions below

0
On

Get rid of this code and it will work:

contactController.displayedPropertyKeys = keysToFetch