Ahad Sheriff Ahad Sheriff - 4 months ago 23
Swift Question

How to hide a Storyboard/ViewControllers forever?

I am working on an app that has two Storyboards: One for my app's onboarding controllers and the other for my main app. My onboarding view controller has a skip button that when pressed directs the user to the main storyboard. I only want to show the onboarding view as long as the user hasn't hit the skip button. Once the skip button is hit, the corresponding storyboard should disappear forever.

I thought I could fix this problem by only making showing onboarding storyboard when the app is first opened, and I found some code online that seemed helpful, however it doesn't work...here is the code that is in my app's AppDelegate:

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

FIRApp.configure()
Fabric.with([Crashlytics.self])

let defaults = NSUserDefaults.standardUserDefaults()
if (defaults.objectForKey("userAsLoggedInBefore") != nil) {

print("Functions")

//here you insert the code to go to the main screen
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "Functions", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("functions") as UIViewController
self.window!.rootViewController = viewcontroller

} else {

//this means the user hasn't logged in before and you can redirect him to the onboarding page
print("Onboarding")

var mainView: UIStoryboard!
mainView = UIStoryboard(name: "Main", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewControllerWithIdentifier("onboarding") as UIViewController
self.window!.rootViewController = viewcontroller

}
}


Note in my code "Main" is the onboarding storyboard, while "Functions" is my main app code.

However this code is not working as intended as my same problem still arises. If anybody could take a look at my code and see if I am doing anything wrong, that would be greatly appreciated.

Thanks in advance!

Answer

You are never writing to NSUser defaults. So it will stay nil..

Your missing some line like this:

let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setValue("Did watch", forKey: "userAsLoggedInBefore")

Put it at the end of if - else statement

Comments