0

I am implementing a Terms & Conditions view into my app and the user has to accept them to proceed, then when they do accept them, they no longer have to go through the Terms & Conditions view. I followed a tutorial on how to integrate UserDefaults and store the value locally if someone does accept the terms. However I am stuck with implementing it into the root view controller. Specifically stuck on my viewDidAppear function. What goes in the if and else statements?

class TermsAndConditionsViewController: UIViewController, UITextViewDelegate {

@IBOutlet weak var termsTextView: UITextView! {
    didSet {
        termsTextView.delegate = self
    }
}

@IBOutlet weak var acceptButton: UIButton! {
    didSet {
        acceptButton.isHidden = true
    }
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    acceptButton.isHidden = scrollView.contentOffset.y + scrollView.bounds.height < scrollView.contentSize.height
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if UserDefaults.standard.bool(forKey: "termsAccepted") {

    } else {

    }
}

@IBAction func acceptButtonTapped(_ sender: Any) {
    performSegue(withIdentifier: "toPeekView", sender: sender)
}

}

Gar
  • 901
  • 1
  • 7
  • 9
  • 4
    Possible duplicate of [How can I show a view on the first launch only?](http://stackoverflow.com/questions/12878162/how-can-i-show-a-view-on-the-first-launch-only) – JAL Jan 23 '17 at 16:32
  • Is it a view should appear in the first ViewController? or is it a separated ViewController? – Ahmad F Jan 23 '17 at 17:05
  • it should appear in the root view controller, then if the terms are accepted, it will check to see if they are and make the next view controller the new root view controller – Gar Jan 23 '17 at 17:07

1 Answers1

4

Probably, you mean "Show a ViewController on First Launch Only".

Using UserDefaults for this purpose is a good idea, however, checking if the term accepted should not be at the TermsAndConditionsViewController layer, instead, it should be in AppDelegate - application:didFinishLaunchingWithOptions, you can decide in it whether the root ViewController should be the TermsAndConditionsViewController or the other ViewController (HomeViewController for example).

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let rootViewController = storyboard.instantiateViewController(withIdentifier: UserDefaults.standard.bool(forKey: "termsAccepted") ? "termsViewControllerID" : "homeViewControllerID")

    window?.rootViewController = rootViewController

    return true
}

TermsAndConditionsViewController:

class TermsAndConditionsViewController: UIViewController {
    //...

    @IBAction func acceptButtonTapped(_ sender: Any) {
        UserDefaults.standard.set(true, forKey: "termsAccepted")

        performSegue(withIdentifier: "toPeekView", sender: sender)
    }

    //...
}

Hope this helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143