Please don't mark my question duplicate. Gone through the following links and tried what they mentioned but no luck.
UserDefaults in IOS 10 is sometimes showing old value
User Default Values Changing to Previous Values Seemingly Randomly - Swift
UserDefaults in IOS 10 is sometimes showing old value
When a user login into the app I am storing some values from login API response in user defaults.
UserDefaults.standard.set(val, forKey: "XYZ")
When the user log out of the app I'm deleting user defaults.
logOutAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in
// Mark:- Function that remove user defaults data
self.resetDefaults()
BackgroundTask().stopUpdate()
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
self.showAlert(title: Constant.projectTitle, message: "Successfully loged out.")
let appDel:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.window?.rootViewController = loginVC
}))
// Reset userdefaults
func resetDefaults() {
//let domain = Bundle.main.bundleIdentifier!
//UserDefaults.standard.removePersistentDomain(forName: domain)
UserDefaults.standard.dictionaryRepresentation().keys.forEach(UserDefaults.standard.removeObject(forKey:))
// let defaults = UserDefaults.standard
// let dictionary = defaults.dictionaryRepresentation()
// dictionary.keys.forEach { key in
// defaults.removeObject(forKey: key)
// }
}
Commented lines are the ways I tried to solve a problem. Please go through following scenario
- Install app
- Log in (user A)
- Log out (user A)
- Log in (user B)
- Kill app from memory (not uninstall)
- Wait for around 20 mins.
- Relaunch app.
Issue - user A's user defaults data restored automatically.
Thanks in advance. Any help surely appreciated, sorry for my English.