1

I have a tab view controller app which will require to show a login view controller when the user is logged out - when he opens the application for the first time or after he logs out.

What is the best approach? where should I put the code which checks for the session and displays the login view controller?

AppDeledate? one of the tabs? A class for the Tab Bar Controller?

I'm looking for some best practices or a working example of such behaviour.

Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Doron Goldberg
  • 643
  • 1
  • 9
  • 23

2 Answers2

1

Best approach is use singleton concept to do this. Use singleton class, and define public method and call it from wherever. It show modularity to your coding. Don't mess this code with viewcontroller or appDelegate( see this post).

Community
  • 1
  • 1
Mani
  • 17,549
  • 13
  • 79
  • 100
1

Singelton design is a good approach. For for a login use case, you really need not have to have a singelton. You can still go with Observer design pattern. Singleton is really nice while using CoreData or persistent storage or many operations that needs a common unique controller across the app.

Here is how you can implement the Observer pattern.

  1. Create A LoginViewController.
  2. In App Delegate, add an observer method that keeps looking for Session Validity & takes care of presenting LoginController.
  3. On viewDidLoad of each TabBarController items, perform the Session validity Check.
  4. If session is not valid, trigger trigger the notification so that the observer can listen to it & respond accordingly.

This is far simpler approach. In combination with Singleton, the Observer pattern provides you a robust scalable approach.

Updated Code

----------------------------------------------------------

Please find LoginObserver Code here

----------------------------------------------------------

Hope that helps.

Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41
  • Thanks for the detailed response. One problem I faced - If the session is dead I want to show the login view controller, which can't be done from the App Delegate. So I need to open it from each view anyway... – Doron Goldberg May 08 '14 at 12:10
  • 1
    No no no. The observer pattern is there for this purpose only. You just send a notification that session is expired. The observer implementation should take care of presenting the loginviewcontroller, based on if the app ran for the first time or has already run. Looks like I should give a sample code to support my answer. I will post it after 2hrs. – Balram Tiwari May 08 '14 at 12:33
  • @DoronGoldberg : Please find attached the updated Code. – Balram Tiwari May 08 '14 at 15:22