0

I've read that the tab bar controller of a project should always be the base controller, but I'm wondering, how do I present my sign in view controller if user is not logged in, then dismiss after user is logged in?

The following is my code

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let loginViewController = storyboard.instantiateViewControllerWithIdentifier("signInViewController") as! SignInViewController

    self.window?.makeKeyAndVisible()
    self.window!.rootViewController!.presentViewController(loginViewController, animated: true, completion: nil)

    return true
}

It does work as expected but I'm having this shown in my console.

2016-01-07 12:37:58.139 Fingers[461:67210] Presenting view controllers on detached view controllers is discouraged <AppName.HomeViewController: 0x13ed4f8f0>.
2016-01-07 12:37:58.149 Fingers[461:67210] Unbalanced calls to begin/end appearance transitions for <UITabBarController: 0x13ed4ed60>.

Also this will trigger the view did load on the root view controller, is there any way to avoid that?

Happiehappie
  • 1,084
  • 2
  • 13
  • 26
  • What problem are you having trying to present the login controller over the tab bar controller? – rmaddy Jan 07 '16 at 04:45
  • just make the login VC appart from all other VC and the tabbar is starting vc, give it an Identifier, check if the user is logged in or not in AppDelegate then present the login VC with its Identifier, thats how i did – Tj3n Jan 07 '16 at 04:49
  • Possible duplicate of [Best practices for Storyboard login screen, handling clearing of data upon logout](http://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login-screen-handling-clearing-of-data-upon-logou) – Nicolas Miari Jan 07 '16 at 04:54
  • @rmaddy I have this 2016-01-07 12:37:58.139 Fingers[461:67210] Presenting view controllers on detached view controllers is discouraged . 2016-01-07 12:37:58.149 Fingers[461:67210] Unbalanced calls to begin/end appearance transitions for . – Happiehappie Jan 07 '16 at 05:07
  • You are going to have to update your question with a lot more details about how you are displaying everything. – rmaddy Jan 07 '16 at 05:08
  • Added my code. Thanks for your time :) – Happiehappie Jan 07 '16 at 05:09

2 Answers2

0

It is very easy to do that using NSNotificationCenter. Just register a notification in your TabBarViewController with a method which presents a login viewcontroller.

let vc = ViewController() //change this to your class name
self.presentViewController(vc, animated: true, completion: nil)

Then if the isLogged flag is NO, you can post a notification to trigger above method.

After logged, just dismiss that.

self.dismissViewControllerAnimated(true, completion: {});
SeanChense
  • 846
  • 8
  • 14
0

Here's a simple example for Firebase using the App Delegate's didFinishLaunchingWithOptions method

application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    var window: UIWindow?
    if Auth.auth().currentUser == nil {
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
            let authVC = storyboard.instantiateViewController(withIdentifier: "AuthVC")
            window?.makeKeyAndVisible()
            window?.rootViewController?.present(authVC, animated: true, completion: nil)
            return true
    }
    //other things to do at launch if applicable
    return true
}
froggomad
  • 1,747
  • 2
  • 17
  • 40