Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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).

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