4

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lesley Peters
  • 409
  • 1
  • 5
  • 20
  • You are close, you would want to present the view controller in the app delegate method didFinishLaunchingWithOptions after you are done with any rootViewController setup. – bolnad May 05 '16 at 23:22

3 Answers3

3

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

Harshit Gupta
  • 1,200
  • 12
  • 27
3

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.

Lesley Peters
  • 409
  • 1
  • 5
  • 20
  • I think you should also disable the (default) automatic loading of your main storyboard by removing the entry in your plist file. – Nicolas Miari May 06 '16 at 03:30
2

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.

lundzern
  • 417
  • 3
  • 11