Question

I have a viewController X (not initial view). X is a childViewController of several parents (several viewController have X as their child.) On X, there is a label, table and a navigation bar with a left bar button that "pops" back to its parent. When the app is launched normally, all the segues, popToParentViewController, back buttons function properly. However, if I was to launch the app from a push notification, X appears with just the navigation bar and button (but does not function) and the table view.

I don't know why the label is not showing! and the back button does not go "back". I know it has to do with "no parent" because I've set it as the "root" in my code below.

How can I implement what I want properly with code?

The following is in appDelegate.m

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                             bundle: nil];
    NotificationsViewController *viewController= [mainStoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
    UINavigationController *nav = [[UINavigationController alloc]
                                   initWithRootViewController:viewController];
    [_window setRootViewController:nav];
}
}
Was it helpful?

Solution 2

First of all, application is always launched with the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method. You shall examine the launchOptions dictionary to check if the app was opened because of a notification. In that launch method you can access the notification object this way:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
{
    // setup navigation controller here, I assume you already have this in your code

    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (dictionary != nil)
    {
        NSLog(@"%@: did launch with notification: %@", [self class], dictionary);

        // put the navigation controllers on the nav controller stack as David described
    }
    [[self window] setRootViewController:navigationController];
    [self.window makeKeyAndVisible];

    return YES;
 }

The method - (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo will be called ONLY if the app had already been launched. It could be called when the app is switching from background to foreground or when it runs. This method is never called when the application starts as a new process.

OTHER TIPS

You'll need to make sure that you create all the view controllers between your root and target view controllers and push them onto the stack, either with a single call to [nav setViewControllers:animated:] or multiple calls to [nav pushViewController:animated:] In either case you'll probably want to make sure the animated parameter is false.

I think you'll also want to make sure that you aren't already someplace inconvenient when you receive the notification, as doing all of the gyrations could be very confusing to the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top