如何在首次运行应用程序时(例如教程)只能使uiviewcontroller可见一次?

时间:2021-03-16 06:13:37

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:

我正在创建一个iOS swift应用程序,我想在用户第一次运行应用程序时显示教程屏幕。稍后,随着应用程序的每次运行,教程应该被隐藏,另一个视图控制器应该是可见的起点。到目前为止,我的故事板看起来像这样:

如何在首次运行应用程序时(例如教程)只能使uiviewcontroller可见一次?

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:

至于现在,在故事板中我选择了标签栏作为初始视图控制器:

如何在首次运行应用程序时(例如教程)只能使uiviewcontroller可见一次?

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?

使用这种方法,从未见过教程屏幕。如何在首次启动应用程序中仅显示一次,然后在每次用户打开应用程序时跳过它?

3 个解决方案

#1


7  

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

在AppDelegate的didFinishLaunchingWithOptions方法中检查NSUserDefaults值就像这样。

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

    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.objectForKey("isFirstTime") == nil {
         defaults.setObject("No", forKey:"isFirstTime")
         defaults.synchronize()
         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.

注意:我创建了ViewController的对象,您需要在将其分配给rootViewController之后创建FirstPage教程屏幕的对象。

#2


2  

For swift 4 make these changes.

对于swift 4进行这些更改。

let defaults = UserDefaults.standard
if defaults.object(forKey: "isFirstTime") == nil {
    defaults.set("No", forKey:"isFirstTime")
    defaults.synchronize()
    ...
}

#3


0  

Simplified Swift 4 version of this answer.

简化了Swift 4版本的这个答案。

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

https://*.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
}

#1


7  

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

在AppDelegate的didFinishLaunchingWithOptions方法中检查NSUserDefaults值就像这样。

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

    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.objectForKey("isFirstTime") == nil {
         defaults.setObject("No", forKey:"isFirstTime")
         defaults.synchronize()
         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.

注意:我创建了ViewController的对象,您需要在将其分配给rootViewController之后创建FirstPage教程屏幕的对象。

#2


2  

For swift 4 make these changes.

对于swift 4进行这些更改。

let defaults = UserDefaults.standard
if defaults.object(forKey: "isFirstTime") == nil {
    defaults.set("No", forKey:"isFirstTime")
    defaults.synchronize()
    ...
}

#3


0  

Simplified Swift 4 version of this answer.

简化了Swift 4版本的这个答案。

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

https://*.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
}