Reputation: 1176
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.
Upvotes: 2
Views: 346
Reputation: 1176
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
}
}
Upvotes: 0