从应用程序设置中的应用程序注销

时间:2016-12-04 06:56:13

标签: ios swift application-settings settings.bundle ios-settings-bundle

我想使用设置包从应用程序注销。

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

//enable_logout key for logout switch identifire in setting budle plist.
        let userLogout =  UserDefaults.standard.bool(forKey: "enabled_logout")
        print(userLogout)

            let userLogin =  UserDefaults.standard.bool(forKey: "isUserLogin")
            if userLogin {

                let homeController = HomeController()
                let homeNav = UINavigationController.init(rootViewController: homeController)


                let aboutController = AboutController()
                let aboutNav = UINavigationController.init(rootViewController: aboutController)

                let userBaseController = UserBaseInfoController()
                let userBaseNav = UINavigationController.init(rootViewController: userBaseController)

                tabbarController.viewControllers =[homeNav,userBaseNav,aboutNav]

                self.window?.rootViewController = tabbarController

            }
            else {

                let login = LoginController()
                self.window?.rootViewController = login


            }



        return true
    }

我在appDelegate中添加了这段代码,我希望当用户在设置中启用logout切换然后返回到应用程序show login视图,但是当启用切换并返回app appDelegate时不调用我的密钥不会更改

有我的设置视图: enter image description here

1 个答案:

答案 0 :(得分:0)

我正在解决此问题,而是在enable_logout方法中检查didFinishLaunchingWithOptions密钥,我检查了applicationWillEnterForeground方法。

这是我的代码:

func applicationWillEnterForeground(_ application: UIApplication) {

        let userLogout =  UserDefaults.standard.bool(forKey: "enable_logout")
        print(userLogout)


        if !userLogout {

            let homeController = HomeController()
            let homeNav = UINavigationController.init(rootViewController: homeController)


            let aboutController = AboutController()
            let aboutNav = UINavigationController.init(rootViewController: aboutController)

            let userBaseController = UserBaseInfoController()
            let userBaseNav = UINavigationController.init(rootViewController: userBaseController)

            tabbarController.viewControllers = [homeNav,userBaseNav,aboutNav]

            self.window?.rootViewController = tabbarController

        }
        else {

            let login = LoginController()
            self.window?.rootViewController = login

        }

    }