从 App Delegate 执行、导航到子视图

Performe, navigate, to child view from App Delegate

当应用程序通过推送通知启动时,我正在尝试打开子视图(PostReaderViewController,Image 上的第四个视图)。故事板图片: 这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      ... 
    //Detecting if the app was lunched by clicking on push notification :
    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];

    if(apsInfo) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        PostReaderViewController* postReader = (PostReaderViewController *)[mainstoryboard instantiateViewControllerWithIdentifier:@"postReaderView"];
        CostumSDPost *tempPost = [[CostumSDPost alloc] init];
        tempPost.ID = userInfo[@"post_id"];
        postReader.thePost = tempPost;
        [self.window.rootViewController presentViewController:postReader animated:YES completion:NULL];
        //userInfo[@"post_id"]);
    }
    return YES;
}

当我通过推送通知启动我的应用程序时,没有显示任何错误,但不幸的是它启动并显示默认视图(图像上的第三个视图)。
请注意,我使用的是 SWRevealMenu 并且初始点(图像上的第一个视图)是 Reveal View Controller

self.window.rootViewController 需要在 viewDidLoadviewDidAppear 中执行演示。如果你早点这样做,那么 rootViewController 将是 nil,或者视图控制器层次结构将不会处于可以容纳演示文稿的状态。

解决这个问题:

首先 我创建了 Global BOOL 变量,然后在 AppDelegate 中我将此 var 设置为 YES,如果应用程序是通过这样的推送通知启动的:

//Detecting if the app was lunched by clicking on push notification :
    NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
    if(apsInfo) { 
        // Set my global Var to YES
        GlobalSingleton *global = [GlobalSingleton sharedInstance];
        global.displayFirstPost = YES; }

然后 在我的主屏幕中,我检查这个变量是否 == YES 然后导航到下一个屏幕自动显示主屏幕:

GlobalSingleton *global = [GlobalSingleton sharedInstance];
             if (global.displayFirstPost) {
                 // NAvigation code to the third Screen
                 }