Search code examples
iosobjective-cnsuserdefaultshockeyapp

How to clear out NSUserDefaults on an updated app


I am using HockeyApp and Ship.io to deploy my ios app to multiple testers on every build. In my latest build, I changed the data types of some objects i was saving into NSUserDefaults, and now it crashes because old data stored on everyones local device clashes with this change. This crash disappears when a person deletes the app and reinstalls from scratch (bc userdefaults get cleared)

I want some kind of clearing logic so that NSUserDefaults get cleared out when people update to latest version. How can I do this?


Solution

  • I would check if an old key exists and if it does reset the defaults using:

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

    (Typed on mobile so sorry if the formatting sucks)