I am working on my app and I am using UserDefaults
to store value of token
@IBAction func loginButtonTapped(_ sender: Any) {
Alamofire.request(Constants.API.url("driver/login"), method: .post, parameters: parameter, encoding: URLEncoding.default, headers: header).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
guard let json = response.result.value as! [String:Any]? else{ return}
guard let userData = json["data"] as! [String: AnyObject]? else{ return}
guard let token = userData["token"] as! String? else { return}
print("The value of token is \(token)")
let user = UserModel(with: userData)
print("The value of \(user)")
UserDefaults.standard.setAccessToken(token)
let userArchiveData = NSKeyedArchiver.archivedData(withRootObject:user )
UserDefaults.standard.setUserData(userArchiveData as AnyObject)
let nav = UIStoryboard.main.instantiate(.mainNavigation) as! UINavigationController
let vc = self.storyboard?.instantiate(.homeVC) as! HomeViewController
nav.pushViewController(vc, animated: true)
break
case .failure(_):
print(response.result.error as Any)
showNormalAlertWithTitle("Invalid Email", message: "Please Provide correct email and password")
break
}
}
}
Here I am storing the token value when login button
is clicked. When I click the logout option it opens the login view controller
, this is the code I wrote in logout function
func logOut() {
UserDefaults.standard.setUserData(nil)
UserDefaults.standard.setAccessToken(nil)
let vc = storyboard?.instantiateViewController(withIdentifier: "mainNavigation")
UIApplication.shared.keyWindow?.rootViewController = vc
}
Now the problem is, after logout when I am going to click login button
again, the login button
is not working, It is not pushing to the HomeViewController
The code I wrote in AppDelegate
class:
func goToRootViewController() {
storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nav = UIStoryboard.main.instantiate(.mainNavigation) as! UINavigationController
if UserDefaults.standard.getAccessToken().isEmpty {
gotoLogin()
return
} else {
let homeVc = UIStoryboard.main.instantiate(.homeVC) as! HomeViewController
nav.pushViewController(homeVc, animated: false)
}
let leftMenuVC = storyBoard?.instantiateViewController(withIdentifier: "menuVC") as! MenuViewController
container = MFSideMenuContainerViewController.container(withCenter: nav, leftMenuViewController: leftMenuVC, rightMenuViewController: nil)
container?.panMode = MFSideMenuPanModeNone
window?.rootViewController = container
window?.makeKeyAndVisible()
}
Any Help?
The issue is that you are setting the rooViewController in logout. So when you push homecontroller again, it is actually pushing the controller but the navigation controller is not the rootViewController now. So you will have to do this in success part:
let nav = UIStoryboard.main.instantiate(.mainNavigation) as! UINavigationController
let vc = self.storyboard?.instantiate(.homeVC) as! HomeViewController
nav.pushViewController(vc, animated: true)
UIApplication.shared.keyWindow?.rootViewController = vc