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

IphoneIosCocoa TouchUikitNsuserdefaults

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

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 - Iphone

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 - Iphone

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 - Iphone

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

Solution 5 - Iphone

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.

Solution 6 - Iphone

Til Xcode 6 and iOS 8 Simulator the location of the plist file changed.

I found the *.plist in the following directory:

[1] /Users/SOME-USERNAME/Library/Developer/CoreSimulator/Devices/SOME-DEVICE-ID/data/Library/Preferences/SP.UserDefaultsTest.plist

Deleting the located file from path [1] manually and the NSUserDefaults are gone.

Solution 7 - Iphone

Here's the swift version:

let domainName = NSBundle.mainBundle().bundleIdentifier!       
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(domainName)

Solution 8 - Iphone

In the simulator top menu:

Simulator -> Reset Content and Settings...

Solution 9 - Iphone

You can use removePersistentDomainForName method available with NSUserDefaults Class.

Syntax :

- (void)removePersistentDomainForName:(NSString *)domainName

Example :

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

Solution 10 - Iphone

If you're doing this in a unittest, you may want to keep the state of your app in the current simulator instead of inadvertently wiping it every time you also run your unittests. One way to do this is to simply hang on to the old values for your app domain in setUp() and then restore them in tearDown():

class MyClass_Tests: XCTestCase {
    static let domainName = Bundle.main.bundleIdentifier!
    static var oldUserDefaults: [String : Any]?

    override class func setUp() {
        super.setUp()
        // Hang onto this so we don't inadvertently wipe the app's state while running tests.
        oldUserDefaults = UserDefaults.standard.persistentDomain(forName: domainName)
    }

    override class func tearDown() {
        // Restore the old values.
        UserDefaults.standard.setPersistentDomain(oldUserDefaults!, forName: domainName)
        super.tearDown()
    }

    override func setUp() {
        super.setUp()
        // Wipe the state for each test.
        UserDefaults.standard.removePersistentDomain(forName: MyClass_Tests.domainName)
    }
    
    override func tearDown() {
        super.tearDown()
    }
}

Solution 11 - Iphone

You can find UserDefaults in following path in Finder, delete the .plist

~/Users/<USER>/Library/Developer/CoreSimulator/Devices/<DEVICE_ID>/data/Containers/Data/Application/<APP_ID>/Library/Preferences/.plist

Path components to replace:

1. <USER> = MAC user name
2. <DEVICE_ID> = Device/Simulator Identifier, e.g., 999271B8-FAA6-41DE-9864-4111F422ED12
3. <APP_ID> = Application identifier, e.g., 69928AEF-BCD5-413A-B06F-BC4A07080D62
4. <APP_BUNDLE_ID> = Your apps bundle identifier, e.g., com.company.appname.plist

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThanksView Question on Stackoverflow
Solution 1 - IphonememmonsView Answer on Stackoverflow
Solution 2 - IphoneTom HarringtonView Answer on Stackoverflow
Solution 3 - IphoneJiulong ZhaoView Answer on Stackoverflow
Solution 4 - IphonedaneView Answer on Stackoverflow
Solution 5 - IphonemmcView Answer on Stackoverflow
Solution 6 - Iphonesys4tronView Answer on Stackoverflow
Solution 7 - IphoneAnkit GoelView Answer on Stackoverflow
Solution 8 - IphoneAndrewView Answer on Stackoverflow
Solution 9 - IphoneJayprakash DubeyView Answer on Stackoverflow
Solution 10 - IphoneqixView Answer on Stackoverflow
Solution 11 - IphoneSaifView Answer on Stackoverflow