添加免责声明屏幕

时间:2017-08-12 23:31:59

标签: ios swift viewcontroller

我正在尝试将disclaimerViewController添加到我的应用中作为初始viewController,如果接受将导致用户输入entryViewController,如果不是,则不允许用户使用该应用程序。但是,我希望如果用户接受免责声明,则在他打开应用程序后的任何时候,他都不会获得disclaimerViewControlelerentryViewController

我知道编辑AppDelegate.swift文件时会有所作为,但我不确定是否要开始。

3 个答案:

答案 0 :(得分:1)

您需要在UserDefaults中保存用户选择

以下代码使用的是Swift 3

如果你不想加载entryViewController,那么在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
}

或者您可以立即加载entryViewController并提供免责声明,然后在您的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      
   }
}

在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
}

答案 1 :(得分:0)

我这样做的方法是将disclaimerViewController设置为storyboard文件中的初始视图控制器。

在swift文件中检查用户之前是否已接受viewDidLoad方法中的免责声明,如果没有,请允许代码正常运行并显示屏幕,如果他们已推送用户到你的entryViewController

如果您想在AppDelgate中而不是在故事板中设置初始视图控制器,这里有一个有用的链接:set initial viewcontroller in appdelegate - swift

要在swift过渡到新的viewcontroller,这是一个有用的链接:Switching view controllers in swift

答案 2 :(得分:0)

实现此目的最流畅的方法是以编程方式切换视图。

确保在StoryBoard中为ViewControllers设置恢复ID。

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()
    }

}

您可以在此处获取完整项目:https://github.com/ryantxr/legendary-fiesta