I am currently working on an application in Xcode 7.2, iOS 9.2 using Swift. I want to have a screen pop up the very first time the user opens the application and get some data and store it permanently. I found a thread where someone asked something similar: Programmatically set the initial view controller using Storyboards have followed the instructions closely, but I can't seem to get it to work the way I want it to.
Here is the modified code from my AppDelegate.swift file.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
let storyboard = UIStoryboard(name: "Main", bundle: nil);
var initialViewController: UIViewController;
print(NSUserDefaults.standardUserDefaults().boolForKey("firstTime"));
if(!NSUserDefaults.standardUserDefaults().boolForKey("firstTime")){
initialViewController = storyboard.instantiateViewControllerWithIdentifier("FirstTimeScreen");
} else {
initialViewController = storyboard.instantiateViewControllerWithIdentifier("Dashboard");
}
self.window?.rootViewController = initialViewController;
self.window?.makeKeyAndVisible();
return true
}
In the ViewController.swift file corresponding to the ViewController that should open from the second time onwards, in the viewDidLoad() method, I have the following line of code:
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "firstTime");
Currently the first screen is always opening. The screen which should from the second time onwards is not the root view controller even though the NSUserDefaults value is tested.
I started the application working on a single view controller, and I currently do not have any Navigation Controllers or anything. Just a bunch of individual View Controllers, connected through button segues.
Thank you for taking the time to read this and possibly answer the question.