1

I have a TabBarController as the App entry point. After a receiving Local-Notification, I want to display a specific UIViewController.

Depending on the actual App-state (frontmost / not-frontmost, not running), I am able to catch the push notification by e.g. application:willFinishLaunchingWithOptions: or application:didReceiveLocalNotification: in the appdelegate.

However, I am not able to manage to open a specific UIViewController at that point (that by the way is not one of the tabbarcontroller.selectedIndexes) .

I tried various things, e.g. presentViewController:

Today*today = [[Today alloc] init];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:today animated:NO completion:nil];

leaving to Error:

Warning: Attempt to present <Today: 0x7fcb91ed5a40> on <UITabBarController: 0x7fcb91c79810> whose view is not in the window hierarchy!

Posts here indicate to use [self.window makeKeyAndVisible]; before calling presentViewController: Same error!

Further posts [ thisOne ] indicate that this error can be omitted by putting presentViewController in viewDidAppear: however, this is not possible in the appdelegate, where i catch the local notification...

Even calling a segue is not possible, as it seems that no View Controller has been loaded at this point already...

Some people indicating that calling a UIViewController from AppDelegate is not possible at all, is this right ?

I am out of ideas, do you have any how to do that.... ?!?

Community
  • 1
  • 1
esvau
  • 744
  • 5
  • 8

2 Answers2

0

I found a solution that works for me:

Make sure that your UIViewController (here Today) is embedded in a NavigationController.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

Today* today=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]  instantiateViewControllerWithIdentifier:@"Today"];

UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:today];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

}

Hope this helps...

esvau
  • 744
  • 5
  • 8
0

If you want to get any other UIViewController, not just the rootViewController:

UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];

UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];
Arben Pnishi
  • 591
  • 5
  • 11