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

后端 未结 4 1723
我在风中等你
我在风中等你 2020-12-01 08:42

I built an application using Xcode 4.5 with storyboards. The first time the app launches I want the initial view controller to appear with terms and conditions that must be

4条回答
  •  旧巷少年郎
    2020-12-01 09:05

    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

提交回复
热议问题