7

I have a logout function in my app. Seems to be a weird problem where it doesn't save the NSUserDefaults. Here I simply want to remove the key. However after logging out if I then open the app again it will find that this key is still in the NSUserDefaults.

func didLogout() {
    // Clear user data
    let settings = NSUserDefaults.standardUserDefaults()
    settings.removeObjectForKey("userData")
    settings.synchronize()

    unregisterForRemoteNotifications()

    openLoginScreen()
}

Any ideas what I could be doing wrong here?

KexAri
  • 3,867
  • 6
  • 40
  • 80
  • please sue about your key `userData`! also before cleanup your NSUserDefult check `objectforkey:"userData"` to sure about existing or no. – Mo Farhand Jan 18 '16 at 08:36
  • Are you sure the method didLogout is being called? Try printing true in the first line to check if it gets called when user logs out. You should also post how you load it. – Leo Dabus Jan 18 '16 at 08:39
  • make sure that your key "userData" is exactly same with proper case sensitivity, because the code you have written works perfectly fine. – Rahul Patel Jan 18 '16 at 08:44
  • 1
    Note: you don't need to force synchronize – Leo Dabus Jan 18 '16 at 08:48
  • Removing a default has no effect on the value returned by the objectForKey: method if the same key exists in a domain that precedes the standard application domain in the search list.----Apple – Lumialxk Jan 18 '16 at 09:15

6 Answers6

4

try this

    DispatchQueue.main.async {
        UserDefaults.standard.removeObject(forKey: YOUR_KEY_HERE)
    }

helped for me

Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
3

removeObjectForKey(_:)

Removing a default has no effect on the value returned by the objectForKey: method if the same key exists in a domain that precedes the standard application domain in the search list.

Just use another key instead of userData. It might exists in another domain.

FranMowinckel
  • 4,233
  • 1
  • 30
  • 26
2

The code above is correct. The key will be still there, but it will return only nil value. So, when user logout you can set

NSUserDefaults.standardUserDefaults().removeObjectForKey("userData")

and when new user login you can set new value by checking

if NSUserDefaults.standardUserDefaults().objectForKey("userData") == nil
Nayana
  • 128
  • 9
2

One of our (XCTest) unit tests was failing every other run. It turned out that -removeObjectForKey: was — inexplicably — only working every other run. I verified this with defaults.dictionaryRepresentation before and after -removeObjectForKey:. I thought perhaps the keys were being added to two domains and the first remove wasn't getting them both (the docs say this can happen) so I cleverly added a second remove, but that also had no effect. My solution was to set the key to the uninitialized value instead.

Any ideas?

David Gish
  • 750
  • 6
  • 14
1

There is no issue in your above code you might have set data in app delegate or when you login your app, or you have mistyped key value.

If you want to clear all data. This will Work

let appDomain = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
ak2g
  • 1,673
  • 15
  • 22
  • please have habit of reading everything properly, as it is mentioned you want to clear all data. This will Work, clearly – ak2g Oct 27 '17 at 14:25
1

I did the following to delete the userdefault of the app on user loggout

private static let userDefaults = NSUserDefaults.standardUserDefaults()
  private static let userTokenKey = "userTokenKey"
 userDefaults.removeObjectForKey(userTokenKey)
    userDefaults.synchronize()
Mr. Bean
  • 4,221
  • 1
  • 19
  • 34