How can I show a view on the first launch only?

In the interests of keeping this question up-to-date, here is a Swift version of the accepted answer.


STEP 1

In your App Delegate, add the following function.

func applicationDidFinishLaunching(application: UIApplication) {
    if !NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "TermsAccepted")
    }
} 

This will essentially set your TermsAccepted Bool to false if this is the first launch (as Bools are false by default).


STEP 2

In your root view controller (the view controller which loads when your app is launched), you must have a way to see if the terms have been accepted or not and act accordingly.

Add the following function.

override func viewDidAppear(animated: Bool) {
    if NSUserDefaults.standardUserDefaults().boolForKey("TermsAccepted") {
        // Terms have been accepted, proceed as normal
    } else {
        // Terms have not been accepted. Show terms (perhaps using performSegueWithIdentifier)
    }
}

STEP 3

Once the user accepts your conditions, you want to change your TermsAccepted Bool to true. So in the body of the method which handles the acceptance of the terms, add the following line.

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "TermsAccepted")

I hope this helps!

Loic

Leave a Comment