Ios 添加免责声明屏幕

Ios 添加免责声明屏幕,ios,swift,viewcontroller,Ios,Swift,Viewcontroller,我正在尝试向我的应用程序添加一个免责声明控件,作为初始的视图控制器,如果接受,将引导用户进入入口视图控制器,如果不接受,将不允许用户使用该应用程序。但是,我想说明的是,如果用户接受免责声明,在他打开应用程序后的任何时候,他都不会看到免责声明控制器,而是看到entryViewController 我知道这与编辑AppDelegate.swift文件有关,但我不确定是否要开始。我要做的是将您的免责声明控制器设置为故事板文件中的初始视图控制器 在swift文件中,检查用户之前是否接受了viewDidL

我正在尝试向我的应用程序添加一个
免责声明控件
,作为初始的
视图控制器
,如果接受,将引导用户进入
入口视图控制器
,如果不接受,将不允许用户使用该应用程序。但是,我想说明的是,如果用户接受免责声明,在他打开应用程序后的任何时候,他都不会看到
免责声明控制器
,而是看到
entryViewController


我知道这与编辑AppDelegate.swift文件有关,但我不确定是否要开始。我要做的是将您的
免责声明控制器设置为故事板文件中的初始视图控制器

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

如果要在AppDelgate中而不是在情节提要中设置初始视图控制器,以下是一个有用的链接:


要在swift中转换到新的viewcontroller,这里有一个有用的链接:

您需要在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      
   }
}
在免责声明C中:

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

实现这一点最平滑的方法是以编程方式切换视图

确保在情节提要中为ViewController设置恢复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()
    }

}
接受视图控制器

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

}

你可以在这里获得完整的项目:

这已经被问过很多次了。询问前请先搜索。下面是一个示例解决方案:您介意解释一下为什么将其标记为重复项吗?您的答案和iOS项目都不支持免责声明。贵宾犬和西班牙猎犬都是狗。你需要做一只狗;这只是一个偶然的事实,它是什么品种。正如我所说,一次性呈现视图控制器(或直到用户满足某些需求)的问题在这里已经得到了大量处理。