kenvu kenvu - 3 months ago 16
Swift Question

iOS Swift Storyboard how to show view after Login and before Login

I'm new to Swift & Storyboard. I'm building an app that need to keep users logged in after they login successfully.
So at the 1st time users open app, I'll show Login ViewController, and after users login, the app shows Home ViewController.

For Objective-C and xib, I just save user login data to Database, and everytime users open the app, I'll check if user data exist in AppDelegate, if it exists I'll show HomeVC.xib otherwise I show LoginVC.xib. Is this correct way to do this?

How can I do this using Storyboard?

Thank you

3li 3li
Answer

You can set the initial view controller programmatically by unchecking the "Is initial view controller" option from your first view controller like so, or simply by deleting the arrow pointing to your first view controller. Then give all view controllers that you want as initial view controllers a different storyboard ID, Like that.

Then set the appropriate view controller to be the initial view controller manually in application:didFinishLaunchingWithOptions method in AppDelegate class:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier((isUserLoggedIn ? "MainVC" : "LoginVC"))
    window?.makeKeyAndVisible()
    return true
}
Comments