0

I have created login page. when I enter email and password returns from Json the user ID and then, I have saved the ID in user default to be able to use it in different pages in my app, but when I added logout button in another page. the function of the logout button is when I press it delete id from user default and then take me to the root uiviewcontroller but when I logout and login with another account steal saving the old ID.

//save id to the user default by using login button
let id = UserDefaults.standard
id.set(data.id , forKey : "ID")
id.synchronize 

//using user id with this code in diferent pages 
let user_id = UserDefaults.standard.integer(forKey : "ID")

//delete id from user default by using logout button
UserDefaults.standard.removeObject(forKey : "ID") 
userDefaults.synchronize 
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainScreen = UIStoryboard.init(name : "Main" , bundle : nil).instantiateViewController(withIdentifier :"BG")
appDelegate.window?.rootViewController =   mainScreen
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Your use of `removeObject` looks correct, so you may have to provide the relevant code in context. Sounds like you may have a problem with the login controller if the old ID is still being used. – Chris Shaw May 29 '19 at 02:29
  • see at: https://stackoverflow.com/questions/58602607/how-to-remove-all-data-by-a-suite-name-from-userdefaults-in-swift/59917261#59917261 and in wipeData set ALL properties to nil – ingconti Jan 26 '20 at 10:06

1 Answers1

0

We have two options one is set empty or any other value corresponding key, otherwise you need delete all user default value means use "removePersistentDomain" key.Example

let appDomain = Bundle.main.bundleIdentifier! UserDefaults.standard.removePersistentDomain(forName: appDomain)