Terry Bu
Terry Bu

Reputation: 899

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?

Upvotes: 0

Views: 1272

Answers (2)

Kane Cheshire
Kane Cheshire

Reputation: 1712

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)

Upvotes: 2

Kex
Kex

Reputation: 8579

what about something like this?

-(void)removeNSUserDefaults {


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults removeObjectForKey:@“yourkey1”];
    [defaults removeObjectForKey:@“yourkey2”];
    //do this for all your keys..
    [defaults synchronize];

}

just remove the objects from the dictionary

Upvotes: 1

Related Questions