Aboogie Aboogie - 3 months ago 24
iOS Question

iOS Build one View

I am building an app(swift iOS9) that requires login and of course it has multiple views after login. Is there a way to only build and run one view that is after the login so that I don't have to enter login information every time?

thanks..

Answer

I assume you're asking how to show different view controllers at launch (ie. login the first time, some other view subsequently). Something along the lines of (untested code):

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let firstVC = (shouldLogin) ? "LoginVC" : "OtherVC"
    let vc = storyboard.instantiateViewControllerWithIdentifier(firstVC)
    self.window?.rootViewController = vc
    self.window?.makeKeyAndVisible() 
    return trud
}

Then NSUSerDefaults is commonly used to store the fact they're logged in, which you can read and set "shouldLogin" appropriately.