To do this in the view controller and not in the applicationβs deletion: Just select the link to the AppDelegate in the view controller and reset its window object with the correct view controller, since it is rootviewController.
Step 1 Make some NSUserDefault that the user can configure. A few buttons, some switches in the form of a table, something. Then, when the user clicks the button, we change the NSUserDefault.
@IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) { defaults.setObject("ViewController2", forKey: "LaunchView") } @IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) { defaults.setObject("ViewController1", forKey: "LaunchView") }
Connect a couple of buttons in the settings view controller to these functions, and we started.
Step 2 Configure the storyboard IDs for all the storyboards that you want to set as the launch view. Thus, for each view controller, which may be the original view controller:
-Wed into your storyboard.
-Click on the view controller.
- In the sidebar on the right, click the icon that looks like the newspaper you are managing the class on.
- In the "Identification" section (third line), select the "Use Storyboard ID" check box (make sure it is turned on), and then type something like "VC1" in the "Storyboard" text box. Make sure you select a different storyboard identifier for each view controller.
-Repeat for each view controller.
Step 3 Configure your initial view controller in the AppDelegate.swift file. Go to the func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool section func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool your application delegate.
Add this for reading from NSUserDefault created earlier:
let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { }
This looks for an NSUserDefault line called "LaunchView" (which you created in step 1) and installs it in a new version of the launch launcher if it finds the corresponding NSUserDefault.
Then, in brackets if let launchview... , we want to check what you installed LaunchView . For each object that you set for LaunchView in step 1 (in the example I made "ViewController2" and "ViewController1" ), you should check it here. So, inside these brackets we add the following:
if launchview == "ViewController2" { } else if launchview == "ViewController1" { }
Then, inside each of these if statements, we add the following code:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
This will open the selected window when your application finishes downloading after it has been in the background for a while.
Your finished didFinishLoadingWithOptions section of your AppDelegate might look something like this: (don't just copy and paste, read the instructions above)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { if launchview == "ViewController1" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } else if launchview == "ViewController2" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } } return true }
Hope this helps you, and many thanks to Ankit Goel, who helped me with this so much. Read more below.
One final note : if you use the switches in the settings view, make sure that in the viewDidLoad of this settings view controller you are reading from the NSUserDefault LaunchView , which the user selected last.