简体   繁体   中英

Logout From application in application Settings

I want to logout from application using 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
    }

I'm added this code in appDelegate, I want to when the user enables logout switch in setting and then return to application show login view, but when enables switch and back to app appDelegate not call and my key not change.

There is my setting view : 在此处输入图片说明

I am solve this problem, instead check enable_logout key in didFinishLaunchingWithOptions method, I checked in applicationWillEnterForeground methods.

Here is my code:

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

        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM