-1

I am trying to add a disclaimerViewController to my application as the initial viewController, which, if accepted will lead the user tot he entryViewController, and if not, will not allow the user to use the application. However, I want to to be that if the user accepts the disclaimer, any time after he opens the application he won't be presented with the disclaimerViewControleler but with the entryViewController.

I know that it has something do to with editing the AppDelegate.swift file, but am unsure of were to start.

marco
  • 656
  • 2
  • 6
  • 22
  • This has been asked MANY times before. Please search before asking. Here's an example solution: https://github.com/mattneub/RegistrationExample – matt Aug 13 '17 at 05:28
  • Would you mind explaining why this was marked as a duplicate? Both your answer and your iOS project do not support disclaimers. – marco Aug 13 '17 at 21:12
  • Both poodles and spaniels are dogs. You need to make a dog; it's merely a contingent fact what breed it is. As I said, the problem of one-time presentation of a view controller (or until the user satisfies some requirement) is heavily dealt with here already. – matt Aug 13 '17 at 21:51

3 Answers3

1

You need to save user choice in UserDefaults

The code below is using Swift 3

If you don't want to load entryViewController then In the AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        //retrieve values from UserDefaults
        //for the first time it will be false, because it was not set earlier
        let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")

        if isAccepted == false {
           //present your disclaimer here
        }else{
           //show entryViewController
        }

        return true
}

Or you can load entryViewController and present disclaimer instantly, then in your entryViewController:

override func viewDidLoad() {
        super.viewDidLoad()

   //retrieve values from UserDefaults
   //for the first time it will be false, because it was not set earlier
   let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")

   if isAccepted == false {
      //present your disclaimer here      
   }
}

In the DisclaimerVC:

@IBAction func accept(_ sender: UIButton){
    //it can be another action in your controller
    //but anyway, you should save user choice after that
    UserDefaults.standard.set(true, forKey: "isAccepted")

    //add code here to dismiss disclaimer
}
Woof
  • 1,207
  • 1
  • 11
  • 21
0

The way I would go about doing this is by setting your disclaimerViewController to the initial view controller in your storyboard file.

In the swift file check to see if the user has previously accepted the disclaimer in the viewDidLoad method, if they haven't, allow the code to function normally and display the screen, if they have push the user on to your entryViewController

If you want to set your initial view controller in the AppDelgate and not in the storyboard, here is a useful link: set initial viewcontroller in appdelegate - swift

To transition to a new viewcontroller in swift, here is a useful link: Switching view controllers in swift

D Bieber
  • 41
  • 3
0

The smoothest way to implement this is to switch the views programmatically.

Make sure to set the restoration ids for your ViewControllers in the StoryBoard.

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var mainViewController: ViewController?
    var acceptViewController: AcceptViewController?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        mainViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainView") as? ViewController
        acceptViewController = mainStoryboard.instantiateViewController(withIdentifier: "AcceptView") as? AcceptViewController
        if mainViewController == nil {
            print("mainViewController is nil")
        }
        if acceptViewController == nil {
            print("acceptViewController is nil")
        }

        let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")

        if isAccepted == false {
            switchToAcceptViewController()
        } else {
            switchToMainViewController()
        }
        return true
    }

    func switchToMainViewController() {
        //mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
        self.window?.rootViewController = mainViewController
        self.window?.makeKeyAndVisible()
    }
    func switchToAcceptViewController() {
        //mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
        self.window?.rootViewController = acceptViewController
        self.window?.makeKeyAndVisible()
    }

}

AcceptViewController

class AcceptViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func acceptAction(_ sender: Any) {
        UserDefaults.standard.set(true, forKey: "isAccepted")
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.switchToMainViewController()
    }

}

You can get the full project here: https://github.com/ryantxr/legendary-fiesta

ryantxr
  • 4,119
  • 1
  • 11
  • 25