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

标签 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 中添加了这段代码,我想当用户在设置中启用注销开关然后返回到应用程序显示登录 View 时,但是当启用开关并返回到应用程序时 appDelegate 不调用并且我的 key 不变。

这是我的设置 View : enter image description here

最佳答案

我正在解决这个问题,而不是检查 didFinishLaunchingWithOptions 方法中的 enable_logout 键,我检查了 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

        }

    }

关于ios - 从应用程序设置中的应用程序注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956241/

相关文章:

ios - Nil 与预期的参数类型不兼容 'NSLayoutAnchor<NSLayoutDimention>'

ios - 如何使用已有的不记名 token 初始化 WKWebView

Azure 连接字符串最佳实践

ios - 用于 IPA 的 Xamarin.Form iOS 项目构建

ios - 使用多维数组作为 CoreML 模型输出

ios - 按下按钮时如何使标签滑到屏幕上? swift 2

c# - 在 C# 中读取默认应用程序设置

c# - 如何从控制台应用程序中访问 Properties 命名空间?

iphone - 如何在iphone中缓存图像

iphone - UIScrollView 无法正确滚动