How to open the pageview controller in my iOS application only when I first launch the application?

I am creating an iOS application and I want to open the pageview controller (not a regular view controller), but only for the first time the user opens the application.

The problem is that I cannot open a new pageview controller in the code. The first screen that users will see is the login screen, but on the first visit, switch to the page view controller.

This is what I still observed in the login screen view manager:

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 has already been created on the storyboard.

+4
source share
3 answers

willFinishLaunchingWithOptions (AFAIK). .

+3

.

appDeligate :

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
}

"" "" - ().

, , .

+2

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
        }
    }

var storyboard:UIStoryboard?

, FinishLaunchingWithOptions, , .

+1

All Articles