I'm in the process of creating an app using xcode/swift that implements both a sliding sidebar menu along with a login/signup process. The sliding sidebar menu functionality should only be accessible on the next view immediately after completing either the login or signup.
I followed the instructions for the Sliding Sidebar tutorial here: Ray Wenderlich - Sidebar Tutorial
Problem is -> My Sidebar menu functionality only works when the containing view is the first view displayed upon opening the app.
Sometimes this is OK because once a user is logged-in, the sidebar menu view is the first view displayed -- but this is not always the case -- Logged-out users must complete signup/login before they are able to access/transition to this view.
I did set the initial view of the app to point to my LoginViewController in the storyboard so I don't know why this would not load first!?
My guess is this has something to do with setting the rootviewcontroller in my AppDelegate.swift file.
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
return true
}
How can I enforce that the loginViewController get displayed first on application startup!?
First of all you need to set your rootViewController as loginVC so in your appDelegate file replace above method with
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// replace names accordingly
let loginViewController = LoginViewController()
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
return true
}
now you should save state of the user in userDefaults or something. For example if the user in not logged in
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn)
and when user login to the app you can update the userDefault like so
NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
so your final function should look like something this
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
if NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn") == true{
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
}
else {
let loginViewController = LoginViewController()
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}
return true
}