Search code examples
iosmultipeer-connectivitypresentviewcontroller

Why does presentViewController not work?


I have created a new project to experiment MultipeerConnectivity. I added these lines to present a viewcontroller in viewDidLoad, but it not comes up. Why? Only a white background I can see.

MCBrowserViewController *browserViewController =
[[MCBrowserViewController alloc] initWithBrowser:browser
                                         session:session];
browserViewController.delegate = self;

self.modalPresentationStyle = UIModalPresentationFullScreen;

[self presentViewController:browserViewController animated:YES completion:
 ^{
     [browser startBrowsingForPeers];
 }];

UPDATE:

I see this in the console:

Warning: Attempt to present MCBrowserViewController: whose view is not in the window hierarchy!


Solution

  • The issue is that you're calling this in viewDidLoad. At that point the view is created but it's not visible anywhere, which means that the view controller has not yet appeared. You need to perform this method on viewDidAppear:.