Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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.

like image 217
Lesley Peters Avatar asked Jan 07 '23 03:01

Lesley Peters


1 Answers

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

like image 87
Harshit Gupta Avatar answered Feb 04 '23 06:02

Harshit Gupta