Nsuserdefaults Not Cleared After App Uninstall on Simulator

NSUserDefaults not cleared after app uninstall on simulator

I think this is due to a bug in the iOS8 Beta Simulator.

The expected behavior is that when the app is deleted, the NSUserDefaults for that app are deleted as well.

  • However, NSUserDefaults are NOT deleted when you remove an app from the simulator.
  • They are correctly deleted when you delete them from a physical device running iOS8.

A quick and annoying solution for now is to click, iOS Simulator -> Reset Content and Settings.

Xcode 9.2 with Simulator 10 still presents this issue. Menu option is now Hardware .. Erase All Content and Settings

I submitted a bug report btw

Deleting app from a device doesn't clear NSUserDefaults

For anyone facing the same issue on device.

If you have more than 1 app under the same group and all of them are using app groups (ON under capabilities), then you will have to remove all the apps from the device in order for the user defaults to be cleared.

Since the user defaults are shared, even if one of the app is on the device then it will not be deleted, as that app will be using the userdefaults.

Clearing NSUserDefaults database programmatically between uninstall/re-install of an app

The NSUserDefaults are cleared automatically when the app is uninstalled, they never persist if you uninstall the app. So you don't need anything to detect that the app is uninstalled the NSUserDefaults would be empty when you reinstall the app.

If you want values to persist between app installation you can use the KeyChain.

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

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.

Are values stored in NSUserDefaults removed when the app that put them there is uninstalled?

No, it will not. I use NSUserDefaults in the exact same manner, and it will not stay after the app is deleted. You can verify this via Organizer if you need to.

It will however persist through updates. I have been using TestFlightApp for all of my beta testing, and the token (and other saved user default data) remains. Hope this helps.



Related Topics



Leave a reply



Submit