Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I make the uiviewcontroller visible only once during first run of the app (e.g. tutorial)?

I'm creating an iOS swift app and I want to show tutorial screen when user runs the app for the very first time. Later on, with each run of the app the tutorial should be hidden and another view controller should be visible as a starting point. So far my storyboard looks like this:

enter image description here

It contains two screens of tutorial (1st and last) and tab bar (which is a main window of my app).

As for now, in storyboard I chose the tab bar to be an initial view controller:

enter image description here

And with that approach the tutorial screen is never seen. How can I show it only once on first launch app and then skip it each time user opens the app?

like image 301
user3766930 Avatar asked Sep 06 '16 15:09

user3766930


1 Answers

In didFinishLaunchingWithOptions method of AppDelegate check for NSUserDefaults value like this way.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.objectForKey("isFirstTime") == nil {
         defaults.setObject("No", forKey:"isFirstTime")
         let storyboard = UIStoryboard(name: "main", bundle: nil) //Write your storyboard name
         let viewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
         self.window.rootViewController = viewController 
         self.window.makeKeyAndVisible()
    }
    return true
}

Note: I have created the object of ViewController you need to create the object of your FirstPage tutorial screen after that assign it to the rootViewController.

like image 67
Nirav D Avatar answered Nov 02 '22 23:11

Nirav D