I'm using storyboard , and I want to open always the same view when user receives remote push notifications, even the app is in background or opened. The view I need to present is four views after the initial view controller set in the storyboard. I read this posts:
How can I show a modal view in response to a notification as a new window? (no parent vc)
Open a specific tab/view when user receives a push notification
So here is my code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
notificacionViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:@"notificacion"];
// First item in array is bottom of stack, last item is top.
navController.viewControllers = [NSArray arrayWithObjects:menu,nil];
[self.window makeKeyAndVisible];
}
But when I receive notification, the app crashes with this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[locationViewController setViewControllers:]: unrecognized selector sent to instance 0x42ccd0'
locationViewController is the view controller set as initial in the storyboard.
Many thanks.
Please try the following code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}
My code differs a bit from the answers I have seen. The fact is that the only code that works form me, is the following:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MenuSlider"];
[navigationController pushViewController:controller animated:YES];
1.- Instantiate de navigationController. Usually the rootviewcontroller in the vast majority of the cases, but not in all
2.- Instantiate the storyboard. Usuarlly tagged as MainStoryboard
3.- Instantiate your specific view controller. You must adapt for your special case
4.- Push as you should do because you've set all you need
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With