2

I'm developing an app for iOS using Swift. I have two view controllers; the first one to configure the app (it must appear only the first time that app is launched), the second view controller is to log in to the app.

The problem is: I want to show the "config controller" at the first launch, and after that the "login controller" must appear, but I don't know how to do this. I tried to put a conditional into the "config controller" to force the second (login) to put it to work. But it doesn't work.

Can you explain me some things about this topic?

1
  • Please post an actual example of what you have tried... Commented Nov 7, 2014 at 0:10

2 Answers 2

1

You can use this piece of code to set which view controller you want to be the first view controller that gets displayed at launch for AppDelegate.h. Just remember to edit the Storyboard ID for your view controllers in Main.storyboard and adjust the strings accordingly

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

    let storyboard = UIStoryboard(name: "MainStoryboard", bundle: NSBundle.mainBundle())
    let defaults = NSUserDefaults.standardUserDefaults()

    var rootViewController : UIViewController;

    if (defaults.boolForKey("HasBeenLaunched")) {
        // This gets executed if the app has ALREADY been launched
        rootViewController = storyboard.instantiateViewControllerWithIdentifier(/*storyboard id of your login controller*/) as UIViewController
    } else {
        // This gets executed if the app has NEVER been launched
        defaults.setBool(true, forKey: "HasBeenLaunched")
        defaults.synchronize()

        rootViewController = storyboard.instantiateViewControllerWithIdentifier(/*storyboard id of your configuration controller*/) as UIViewController
    }

    self.window?.rootViewController = rootViewController;
    self.window?.makeKeyAndVisible();

    return true
}

This function gets called as soon as your application is finished launching and is about to display its initial view controller. To get a better understanding of what the if is checking for check the documentation for NSUserDefaults (it's a really useful class). The last two statements before the return do is basically displaying the view controller that has been selected inside the if.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use NSUserDefaults or something else for save your configuration or just save the fact your app is already configured and in your appDelegate in didFinishLaunchingWithOptions you do a simple if for load the controller you want. But my solution doesn't use the storyboard.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.