1

I develop an iOS app and my initial view controller shows a login form. Then when the user successfully authenticates the actual application is presented as a modal view controller.

When the user logs out the application it is dismissed and the login view controller is visible again.

My problem is: When the application is started again and the user already is logged in I don't want to show the login view controller. I can't present the view controller before the login view controller is fully shown:

Warning: Attempt to present <UITabBarController: 0x10a271b40> on <LoginViewController: 0x10a2594f0> whose view is not in the window hierarchy!

I can present it with a delay but this is not what I want.

I could check if the user is logged in before I show a view (in the app delegate) and then show the login or the application. The problem with this is: When the user logs out I have no login view controller beneath my application to which I can pop.

I can't find a good solution except defining the actual application as the root and present the login over it. But this brings a lot new problems because the main application needs data structures I can only initialize after login.

Alexander Theißen
  • 3,799
  • 4
  • 28
  • 35
  • Check my Answer **http://stackoverflow.com/questions/16351348/example-for-login-screen-modally-based-on-storyboard/16351631#16351631** and you have to do as said by @Wain. – Kumar KL Feb 03 '14 at 10:59
  • Seems like you have done it the complete opposite way – CW0007007 Feb 03 '14 at 11:24

2 Answers2

3

You don't want to be running with a modal always shown, it will be easy to inadvertently dismiss your 'entire app'.

Instead, do as you suggest in the app delegate where you decide which view controller is required and change the root view controller of the window. Again, when the user logs in / out, change the windows root view controller.

Wain
  • 118,658
  • 15
  • 128
  • 151
2

Most Application flow will be like that.

1) Set your tabbar controller as rootviewcontroller. When application launch, check with login status and show your tabbar(rootviewcontroller) if already logged in. Otherwise present modal view as loginview controller from tabbar(tabbar is rootviewcontroller).

2) When logout success, again show loginView as modal view(tabbar is rootviewcontroller).

Note: You can also shift rootviewcontroller as tabbar and loginview. But I recommented above flow.

Mani
  • 17,549
  • 13
  • 79
  • 100
  • Presenting a modal login viewcontroller is a great way to think about it. That way you can easily control access to the rest of the app. – dustinrwh Jun 03 '16 at 21:29