1

I am really new to IOS so I apologize if this questions is not worded clearly. I have tried searching around but I have not found exactly what I am looking for.

basically in my AppDelegate applicationDidBecomeActive method, I am making a call to my webservice to make sure that the user is still a valid user, and to pull down some refrehsed data, or kick them back to the login page if they are no longer valid.

The part that I am having trouble with is the second part. How can I load and show and specific ViewController(in this case the loginViewController) when the user is found to be invalid? I want to let the normal viewController flow happen when they are valid, which is is doing fine, but I can not figure out how to launch a specific viewController when I need to from AppDelegate.

Any ideas?

I think I got it! I used this code in the AppDelegate to display the ViewController I needed.

UIViewController *loginController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    UINavigationController *loginNavController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginNavController"];
    UIViewController *currentVC = self.window.rootViewController;
    _window.rootViewController = loginNavController;
    [currentVC presentViewController:loginNavController animated:NO completion:nil];
rysulliv
  • 11
  • 3
  • I believe this is answered here: http://stackoverflow.com/questions/8186375/storyboard-refer-to-viewcontroller-in-appdelegate – Sani Elfishawy Sep 10 '14 at 18:42

1 Answers1

0

For simplicity, lets say you have a one view app (not nav controller, not tab bar controller - the solution scales but easier to explain). When you get the appDelegate message that the app launched, then make a UIImageView the root view and show your launch image (user thinks you are still booting up). Try to log in, and do this in some other object (not a view controller). If you succeed, you make your desired view the rootView, and users sees it. If the login fails, then you makea login window the rootView. The key here is to have an object that is driving this and can interact with the appDelegate. You could also add this functionality to the appDelegate itself.

David H
  • 39,114
  • 12
  • 86
  • 125
  • I am pretty sure that is the logic that I am following, however I do not know how display a specific view from that object or the appDelegate(which is where I am doing it now for simplicity in testing). Sorry I forgot to mention that I am using Navigation Controls so not sure if that would change the code. My loginViewController has a NavigationController it is connected to and that is the view I would like to launch. – rysulliv Aug 14 '12 at 23:17
  • I use the appDelegate to switch views in a nav controller inside a tabbarController. The key here is the "viewControllers" array. You can create the first array with the UIImageView as described above, then later you set the viewControllers array to an array with one object, the loginViewController or something else. – David H Aug 14 '12 at 23:25
  • Sorry I am just now following you I don't think. Maybe this source code that I have been trying will help. [code] LoginViewController *loginViewController = [[LoginViewController alloc]init]; UINavigationController *navController = [loginViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginNavController"]; navController.view = loginViewController.view; _window.rootViewController = navController; [code] – rysulliv Aug 14 '12 at 23:41
  • sorry for the life of me I can not figure out how to put code blocks in here. – rysulliv Aug 14 '12 at 23:49
  • Edit your original question. Paste the code at the bottom of what is there. Then select that text and tap the "()" icon at the top of the text block. – David H Aug 15 '12 at 00:04