When I click the notification, I want it to open a notification viewController of the app. All codes are in AppDelegate.m
I have a function to find the topMostController
- (UIViewController *)topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
Then there's another function to display the notification viewController
UINavigationController *navigationController = (UINavigationController *)[self topMostController];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
MyViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[navigationController pushViewController:myController animated:YES];
It works fine if the app is open. However, when the app is closed, it will crash if I click the notification. The error is
Fatal Exception: NSInvalidArgumentException
-[ViewController pushViewController:animated:]: unrecognized selector sent to instance
I think there's no topMostController or the navigationController is invalid in such. I need to use the UINavigationController because I want a "back" button to go back to a page (main page) rather than closing the app. How could I make the topMostController valid in such case? Should I create something inside didFinishLaunchingWithOptions function? Thanks.