Presenting CNContactViewController viewControllerForContact

Hi,

I am trying to display a CNContactViewController as a presentation, since pushing it on a navigation controller doesn't work for me in a splitView iPad app. I use this code to create and present the view controller:

CNContactViewController *createContact = [CNContactViewController viewControllerForContact: matchedContact];
createContact.delegate = self;
createContact.allowsActions = NO;

CNContactStore *store = [[CNContactStore alloc] init];
createContact.contactStore = store;
     
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:createContact];
navigation.modalPresentationStyle = UIModalPresentationPageSheet;
     
self.contactViewController = createContact;
     
[self presentViewController: navigation animated:YES completion:nil];


This displays the CNContactViewController correctly, but it only has an "Edit" button in the bar button items. It doesn't show a "Cancel" or "Done" button that would allow the user to dismiss the presented view controller. I can try adding it a cancel button in the left bar button items inside the completion handler after it's presented, but if the user presses Edit and then completes the edits, the left bar button item disappears.


Can the [CNContactViewController viewControllerForContact:] be presented, or does it always have to be pushed? If it can, then why isn't it working right now?

I've been struggling with this stuff as well, see:


http://www.openradar.me/radar?id=6729385394569216


There doesn't seem to be any combination of presentation contexts that make CNContactViewController happy, at least as of b5.


g.

I managed to get it working for most situations, I think:


if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: self.contactViewController];
        navigationController.modalPresentationStyle = UIModalPresentationPageSheet;
        self.contactViewController.navigationItem.leftBarButtonItems = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target:self action:@selector(dismissContactsViewController:)]];
   
        [self presentViewController: navigationController animated:YES completion: nil];
    } else {
        [self.navigationController pushViewController:self.contactViewController animated:YES];
    }


The delegate can then dismiss it using this:


        if (self.contactViewController.presentingViewController) {
            [self.contactViewController dismissViewControllerAnimated: YES completion:nil];
        } else {
            [self.navigationController popViewControllerAnimated:YES];
        }


It's not perfect, but works for all intents and purposes ... but this was a couple of weeks ago, so not sure if there were some problematic edge-case that I don't remember now.


Hope it helps!

Presenting CNContactViewController viewControllerForContact
 
 
Q