Search code examples
iosios8nsuserdefaultsudid

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:

  • I recognize that NSUserDefaults are by default, meant to be long lived, and so likely, I have to build some logic of my own to detect this scenario.
  • Since I am using Core Data, I will likely create an entity that stores these reference data timestamps, instead of using NSUserDefaults. This way, a fresh empty database is a giveaway that all reference data needs a fresh load.
    • I am toying with building a more generic JSON-->Core Data importer that I could use across apps with similar reference data needs. Thus, I was hoping to avoid tying things to a Core Data instance and leveraging NSUserDefaults.
  • Post development phase, I will be packaging a SQLite database and copying into place instead of importing flat files into the database.

Solution

  • 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.