1

I'm checking to see if a user is logged in. If logged in, then skip the LoginViewController (Initial View Controller), and head over to the MainViewController.

I've tried doing this check within LoginViewController in countless places of the lifecycle without the under-layer flashing.

By under-layer flashing, I mean you can see the LoginViewController for a split second before the MainViewController comes up.

I have solved this 1 way, but it feels extremely hacky:

In my LoginViewController:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    calls++

    if calls == 2 && NSUserDefaults.standardUserDefaults().objectForKey("AuthToken") != nil {
        self.performSegueWithIdentifier("loginUserNoAnim", sender: self)
    }
}

The "loginUserNoAnim" is a custom Segue I created with no animation.

viewDidLayoutSubviews() gets called in my code 3 times. The first two times, I get warnings that my ViewController is not in the window hierarchy similar to this: Warning: Attempt to present <PSLoginViewController: 0x1fda2b40> on <PSViewController: 0x1fda0720> whose view is not in the window hierarchy! This creates a two extremely quick white flashes in the UI. I got rid of them by checking how many calls were made via the call variable.

Now, this is 100% hacky and not acceptable.

Is there a way to solve this issue by using the iOS lifecycle? I've even tried using the appdelegate method:

 func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

But I still saw the under-layer flash. What is the best way to mitigate it?

David
  • 6,021
  • 8
  • 39
  • 85
  • In fact, my "hack" is flawed in that you still see it, but hardly .01 seconds... but again, it is there. – David Feb 10 '15 at 06:41

1 Answers1

0

I think the following StackOverflow answer is a good explanation how to switch between login and main view controllers at the application start:

Programmatically set the initial view controller using Storyboards

Note that if you provide sign out functionality in the app, you should then direct user to the log in view controller as it is checked only on application start.

Community
  • 1
  • 1
Teemu Kurppa
  • 4,621
  • 2
  • 29
  • 37
  • The problem with those methods is they don't use a segue explicitly. For some reason, I need to use a segue for this. I'd also rather manage this somehow through the iOS life-cycle – David Feb 10 '15 at 07:56