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

后端 未结 3 1474
攒了一身酷
攒了一身酷 2020-12-29 16:01

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 tutoria

相关标签:
3条回答
  • 2020-12-29 16:43

    Simplified Swift 4 version of this answer.

    https://stackoverflow.com/a/39353299/1565913

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    
        if !UserDefaults.standard.bool(forKey: "didSee") {
             UserDefaults.standard.set(true, forKey: "didSee")
    
             let storyboard = UIStoryboard(name: "Main", bundle: nil) 
             let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewController")
             self.window?.rootViewController = viewController 
             self.window?.makeKeyAndVisible()
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-12-29 16:49

    For swift 4 make these changes.

    let defaults = UserDefaults.standard
    if defaults.object(forKey: "isFirstTime") == nil {
        defaults.set("No", forKey:"isFirstTime")
        defaults.synchronize()
        ...
    }
    
    0 讨论(0)
  • 2020-12-29 17:00

    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.

    0 讨论(0)
提交回复
热议问题