Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a key from NSUserDefaults not working

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?

like image 333
KexAri Avatar asked Jan 18 '16 08:01

KexAri


People also ask

How do you reset NSUserDefaults?

In the simulator top menu: Simulator -> Reset Content and Settings... Show activity on this post. You can use removePersistentDomainForName method available with NSUserDefaults Class.

Which method is used to remove all keys from user default?

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

How do you check if NSUserDefaults is empty in Objective C?

There isn't a way to check whether an object within NSUserDefaults is empty or not. However, you can check whether a value for particular key is nil or not.

How do I delete data from UserDefaults?

To remove a key-value pair from the user's defaults database, you need to invoke removeObject(forKey:) on the UserDefaults instance. Let's update the previous example. The output in the console confirms that the removeObject(forKey:) method works as advertised.


5 Answers

try this

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

helped for me

like image 177
Adam Smaka Avatar answered Oct 18 '22 02:10

Adam Smaka


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.

like image 21
FranMowinckel Avatar answered Oct 18 '22 02:10

FranMowinckel


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
like image 2
Nayana Avatar answered Oct 18 '22 00:10

Nayana


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?

like image 2
David Gish Avatar answered Oct 18 '22 02:10

David Gish


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)
like image 1
ak2g Avatar answered Oct 18 '22 00:10

ak2g