idStar
idStar

Reputation: 10794

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

Goal:

Cleaning an app's NSUserDefaults after the app has been uninstalled. Conversely, on re-install of an app, having it ignore any previous NSUserDefaults that might have been stored. That is, being able to detect that those NSUserDefaults belong to an earlier install that was since uninstalled.

Note that the uninstalled and re-installed version could be the exact same build/version number.

Context:

I'm storing some key-value pairs that represent timestamps for the load of specific reference data files, so the my app can simply skip re-loading reference data that hasn't changed.

During development, I sometimes remove the app from the simulator or device, but don't want to have to reset all settings to clear out NSUserDefaults.

I realize there are a host of things I can put in place to determine if my reference data load is complete and up to date. My goal is to determine whether between installs / uninstalls of my app, this fact can easily be determined.

Once reference data is installed (loaded), that's not the end of the story, as new patches will exist and based on their timestamp, I'll need to determine whether to ignore or whether to do an update-or-create style import.

Approaches Investigated:

  1. UDID Replacements. This post explains how one can generate their own UDID like replacement. In the cited context, the fact that this value can change on uninstall is seen as a negative. While, in my context, it would be a positive. I could then nest all my install specific keys behind a dictionary keyed by this unique identifier. However, if an app is installed on a device with at least one more apps from that same developer, the unique identifier will persist through an uninstall / re-install cycle.
  2. Detecting Re-install after Uninstall. This I could not find a ready answer to. Is there any more generalized way for an app to detect that it is freshly installed, distinguishing this from a re-install after uninstall?

Approaches I Will Likely Employ:

Upvotes: 1

Views: 641

Answers (2)

Rami
Rami

Reputation: 179

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.

Upvotes: 1

idStar
idStar

Reputation: 10794

Instead of using NSUserDefaults, an app can just read/write it's own property list file that will get blown away with an uninstall.

We can store a timestamp in that property list for first launch, so that an app can determine when this current install was applied. The same key in the NSUserDefaults database would represent the first install of the app ever. Two similar, but distinct bits of information.

Combining the two, we can then store a count in the NSUserDefaults of how many times an app has been completely re-installed, if we wanted to know.

Aside: writing out a problem certainly does help clarify an issue in one's own mind!

Upvotes: 0

Related Questions