5

I am using SWRevealViewController in my project, and I want to open a particular controller when the app receives a notification. I have tried so many solutions but nothing works.

I follow this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard. My storyboard is designed as below:

Storyboard

When the application receives a notification I want to load Photo view controller within its navigation controller. I tried with the following code in the AppDelegate:

UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: @"photoView"];
    UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
    SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: @"menuController"];

    SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]  init];

    mainRevealController.rearViewController = rearViewController;
    mainRevealController.frontViewController= frontNavigationController;
    self.window.rootViewController =nil;
    self.window.rootViewController = mainRevealController;
    [self.window makeKeyAndVisible];

This works, but creates a new Navigtion controller, and what I need is to use the one already defined in the storyboard, since it has specific properties.

Any idea?

Thanks

Kepa Santos
  • 557
  • 7
  • 21

3 Answers3

7

actually SwLRevalViewController Taken the ownership of Root, so do like this

in Appdelegate.m

//initially navigate to your Main controller on SWL

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{

   NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
 [[NSUserDefaults standardUserDefaults]synchronize];
 UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];

    self.window.rootViewController = main;
 }

//it automatically redirect to your root controller

on that main view controller ViewDidLoad check / Navigate to your photo view controller

        NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
 if ([gettypeofItem isEqualToString:@"something"])
        {
   yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];

   [self.navigationController pushViewController:ivc animated:YES];
    }
  else

  {

   // do nothing 

  }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
2

You can use following method

NSString *storyBoardName=@"Main_iPad";
if (isPhone)
    storyBoardName=@"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];

PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:@"playVideoUsingSockets"];


float disptachTime=2.0;
if (value)
{
    disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[[[UIApplication sharedApplication] keyWindow] rootViewController]  presentViewController:controller animated:YES completion:nil];
Arslan Asim
  • 1,232
  • 1
  • 11
  • 29
2

please try this it may help you. in this when you click on notification this method us called first insted for appdelegate

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
UINavigationController *navController = (UINavigationController  *)self.window.rootViewController;

NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];

[navController.visibleViewController.navigationController pushViewController:notificationViewController];    

}
Akash Raghani
  • 557
  • 1
  • 9
  • 21