How can I reset the NSUserDefaults data in the iPh

2019-01-13 04:06发布

问题:

I've added NSUserDefault 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.

回答1:

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.



回答2:

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.



回答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


回答4:

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.



回答5:

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

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



回答6:

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.



回答7:

Here's the swift version:

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


回答8:

In the simulator top menu:

Simulator -> Reset Content and Settings...


回答9:

You can use removePersistentDomainForName method available with NSUserDefaults Class.

Syntax :

- (void)removePersistentDomainForName:(NSString *)domainName

Example :

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


回答10:

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()
    }
}