简体   繁体   中英

How to open a page view controller in my iOS app only the first time the app runs?

I am creating an app for iOS and I want to open a page view controller (not a normal view controller) but only the first time that a user opens the application.

The problem is that I can't open a new page view controller within the code. The first screen that the users will see is the login screen, but on first visit switch to the page view controller.

This is what I have so far in the login screen viewcontroller:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")
    if launchedBefore  {
        //Not the first time, show login screen.
    }
    else {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
    }
    let secondViewController:InstructionViewController = InstructionViewController()

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

The page view controller that I want to open is already created on the storyboard.

With the other answers I was able to solve the problem.

In the appDeligate file you need to replace the first function with this:

var window: UIWindow?
var storyboard:UIStoryboard?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    window =  UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.makeKeyAndVisible()

    let launchedBefore = NSUserDefaults.standardUserDefaults().boolForKey("launchedBefore")

    if launchedBefore  {
        //Not the first time, show login screen.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewControllerWithIdentifier("Login")

        if let window = self.window {
            window.rootViewController = rootController
        }

    }
    else {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewControllerWithIdentifier("Instruction")

        if let window = self.window {
            window.rootViewController = rootController
        }
    }

    return true
}

"Login" and "Instruction" are the names of the (page) view controllers.

I am not sure if this is most robust code but it works fine for me.

App delegate's willFinishLaunchingWithOptions method would be the best place (AFAIK). check the condition and set window's root view controller accordingly.

Here is Lesley's code updated to Swift 3:

    window =  UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()

    let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")

    if launchedBefore  {
        //Not the first time, show login screen.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewController(withIdentifier: "webView")

        if let window = self.window {
            window.rootViewController = rootController
        }

    }
    else {
        UserDefaults.standard.set(true, forKey: "launchedBefore")
        //First time, open a new page view controller.
        storyboard = UIStoryboard(name: "Main", bundle: nil)
        let rootController = storyboard!.instantiateViewController(withIdentifier: "progressView")

        if let window = self.window {
            window.rootViewController = rootController
        }
    }

Don't forget to set the

 var storyboard:UIStoryboard? 

Before didFinishLaunchingWithOptions, and to set the identifiers similar to your own view controllers identifiers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM