How can I reset the NSUserDefaults data in the iPhone simulator?

I've added NSUserDefaults data retrieval to my app, which is pretty nice. But for testing I would like to reset all the data I added to the defaults database, so that everything is in the state when the user launches the app the first time.

I tried to call:

[NSUserDefaults resetStandardUserDefaults];

but that doesn't do anything. The defaults are still saved and can be retrieved.


Solution 1:

You want NSUserDefaults removePersistentDomainForName. This will remove all user defaults for the application:

NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

For more information on the NSUserDefaults class see the Apple docs.

Alternately, if all you are concerned with is data in the iOS Simulator, you can do that via iOS Simulator > Reset Content and Settings.

enter image description here

Solution 2:

The easiest way is to remove the app from the simulator-- just like you'd remove it from a real phone, by tapping (clicking) and holding until the icons start vibrating. That removes all app data, and the next time you install from Xcode it's like the first time.

If you have other app data you need to keep, you have a couple of options.

One way would be to have some debug code that calls removeObjectForKey: on each of your defaults keys.

The other is to find the directory where the simulator copy is installed, and remove the file containing the preferences. Use this to find the app:

ls -ld ~/Library/Application\ Support/iPhone\ Simulator/User/Applications/*/*.app

The full path to your app will contain directory whose name is a UUID. In that directory, look in Library/Preferences for the preferences file. Remove that, and user preferences are gone.

Solution 3:

You may find out what you have "written" to userdefaults for the app are all inside a file delete this .plist file works:

user name/Library/Preferences/com.theAppYouAreHandling.plist

Solution 4:

In Swift 2.0 the following 1 line will reset all the NSUserDefaults for an app:

NSUserDefaults.standardUserDefaults().removePersistentDomainForName(NSBundle.mainBundle().bundleIdentifier!)

Solution 5:

Actually, this may not be suitable in every circumstance, but since I keep no information of value in the simulator, I just Reset Content and Settings from the iPhone menu, from within the simulator itself.