Search code examples
iosswift2bwwalkthrough

Welcome Screen on Launch


I want a way for the user to have a welcome screen / tutorial on the first launch of the app. If it isn't the first launch of the app, then it open as it usually would.

I already have the welcome screen tied to a button function if the app opens normally. I'm using BWWalkThroughViewController. Here's my code for the button function:

@IBAction func showWalkThroughButtonPressed() {

    // Get view controllers and build the walkthrough
    let stb = UIStoryboard(name: "MainStoryboard", bundle: nil)
    let walkthrough = stb.instantiateViewControllerWithIdentifier("walk0") as! BWWalkthroughViewController
    let page_one = stb.instantiateViewControllerWithIdentifier("walk1") as UIViewController
    let page_two = stb.instantiateViewControllerWithIdentifier("walk2") as UIViewController
    let page_three = stb.instantiateViewControllerWithIdentifier("walk3") as UIViewController
    let page_four = stb.instantiateViewControllerWithIdentifier("walk4") as UIViewController
    let page_five = stb.instantiateViewControllerWithIdentifier("walk5") as UIViewController

    // Attach the pages to the master
    walkthrough.delegate = self
    walkthrough.addViewController(page_one)
    walkthrough.addViewController(page_two)
    walkthrough.addViewController(page_three)
    walkthrough.addViewController(page_four)
    walkthrough.addViewController(page_five)

    self.presentViewController(walkthrough, animated: true, completion: nil)
}

func walkthroughCloseButtonPressed() {
    self.dismissViewControllerAnimated(true, completion: nil)
}

That code is located in the MyTableViewController.swift file.

Here's what I can't figure out:

I want the view controllers to show on first launch. Once the user finishes the tutorial, they can press the Close button and it will close. I have the code to check if it's the app's first launch. It's located in the AppDelegate.swift file. Here's that code:

// First Launch Check

    let notFirstLaunch = NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunch")
    if notFirstLaunch {
        print("First launch, setting NSUserDefault")
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "FirstLaunch")
    }
    else {
        print("Not first launch.")
    }
    return true

So how do I get the welcome screen to launch on first launch? Do I have to create a function in AppDelegate to handle that, and if so what do I have to do to make the tutorial the initial view controller for just the first launch?


Solution

  • I believe what you need to do is already covered here: Programmatically set the initial view controller using Storyboards. If that doesn't work for you add more notes on why the implementation failed. A google search on "programatically change uiviewcontroller on launch ios" will yield other similar links.