我想知道,是否可以检查用户是否打开了应用程序的每个viewcontroller
我想这样做,因为我给用户徽章,这是我想给。
我想我必须将一些东西存储到userDefaults中,然后以某种方式收集信息,然后做我想做的事情,对吗?如果我是对的,那么每次用户打开新的viewcontroller,我应该做一些全局变量并添加计数吗?
任何信息都很感激。

最佳答案

设置一个选项来表示每个viewController。在每个viewControllersViewDidAppear中,从存储显示的viewControllers选项集的Userdefaults读取并更新一个字段,然后将其写回Userdefaults。

    struct UserDefaultsKey {
        static let displayedViewControllers = "displayedViewControllers"
    }

    struct DisplayedViewControllers: OptionSet {
        let rawValue: Int

        static let vc1 = DisplayedViewControllers(rawValue: 1 << 0)
        static let vc2 = DisplayedViewControllers(rawValue: 1 << 1)
        static let vc3 = DisplayedViewControllers(rawValue: 1 << 2)
        static let vc4 = DisplayedViewControllers(rawValue: 1 << 3)
        static let all = [vc1, vc2, vc3, vc4]
    }

    class vc1: UIViewController {
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(true)
            var displayedViewControllers = DisplayedViewControllers(rawValue: UserDefaults.standard.integer(forKey: UserDefaultsKey.displayedViewControllers))
            displayedViewControllers.insert(.vc1)
            UserDefaults.standard.set(displayedViewControllers.rawValue, forKey: UserDefaultsKey.displayedViewControllers)
        }
    }

    func haveAllViewControllersBeenDisplayed() -> Bool {
        let displayedViewControllers = DisplayedViewControllers(rawValue: UserDefaults.standard.integer(forKey: UserDefaultsKey.displayedViewControllers))
        for controller in DisplayedViewControllers.all {
            if displayedViewControllers.contains(controller) == false {
                return false
            }
        }
        return true
    }

关于ios - 检查用户是否打开了每个ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40554643/

10-12 00:30