Question

The situation is :

  • App is on background
  • The user click on icon app
  • App open and show the view controller where we were before apps entered background last time.

I'd like to know which view controller is about to be presented. I'm looking for something like :

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if ([self.window.viewControllerOnScreen isKindOfClass:[HomeViewController class]]) {
        //do sthg
    }
}

Because in case, it's the home view controller (embed in a navigation controller and i use storyboards) i would perform some reload method.

Was it helpful?

Solution 2

As per this link Each object receive a UIApplicationDidEnterBackgroundNotification notification when the app goes in background. Similarly UIApplicationWillEnterForegroundNotification gets fired when app comes in foreground.

so you can use it to keep track of which view controller is opened when app enters foreground

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appEnteredForeground:)
                                             name:UIApplicationDidEnterForegroundNotification
                                           object:nil];

OTHER TIPS

[[self.navigationController viewControllers] lastObject];

The first part will give you an array of all of the viewControllers on the stack, with the last object being the one that is currently display. Check its class type to see is it the homeViewController

Try this

 - (UIViewController *)topViewController{
         return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
 }

     - (UIViewController *)topViewController:(UIViewController *)rootViewController
    {
     if (rootViewController.presentedViewController == nil) {
     return rootViewController;
     }

     if ([rootViewController.presentedViewController isMemberOfClass:[UINavigationController class]]) {
          UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
          UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
     return [self topViewController:lastViewController];
     }

      UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
     return [self topViewController:presentedViewController];
    }

I have done this for getting the current viewController

if (![[appDelegate.rootNavController topViewController] isMemberOfClass:NSClassFromString(@"LGChatViewController")]) {}
self.tabBarController.viewControllers = [NSArray arrayWithObjects: [self LoadAccount], [self LoadContacts], [self LoadPhoneLine], [self LoadSettings], nil];

if you use tab bar then you set like this and show first account and go on......

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