CoolDocMan
CoolDocMan

Reputation: 638

iOS: What are the possible options to Reset an iOS apps Data without requiring a reinstall?

This is a iOS 101 question and I feel like an ass for not knowing the answer :-) The question is - how do I reset an iOS app to the state it was when first installed? The app stores content in NSUserDefaults and plists. It appears excessive to have to ask the user to delete and reinstall the app.

What are the possible options available aside from asking my user to simply reinstall the app (which would makes me look kinda un-innovative :-) )

This particular app used NSUserDefaults and plists to store data. But while you come up with your creative solutions I'd appreciate any clues for other situations as well (i.e. where data is in core data and .plists)

Also if this is a feature needs to be implemented in the app code, appreciate suggestions on a elegant solution for it.

Thanks!

Upvotes: 3

Views: 2861

Answers (2)

user529758
user529758

Reputation:

Delete all files from the sandbox:

NSString *p = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
for (NSString *fname in @[ @"tmp", @"Library", @"Documents", @"Caches", @"Preferences" ]) {
    NSString *path = [p stringByAppendingPathComponent:fname];
    [[NSFileManager defaultManager] removeItemAtPath:path error:NULL];
}

Alternative way for resetting the user defaults only:

NSDictionary *dRepr = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
for (NSString *key in dRepr) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}

[[NSUserDefaults standardUserDefaults] synchronize];
[NSUserDefaults resetStandardUserDefaults];

Upvotes: 5

Manu
Manu

Reputation: 788

The NSUserDefaults is a keyValue collection so for all the keys that you use across the app you can call this method:

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"yourKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

about plist I suppose that you are talking about writing NSArrays and NSDictionaries on the disk's device or implementing NSCoding delegate in your objects subclasses and archiving them... in this case just remove the files using the file manager

NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:@"filePath" error:&error];

for core data it's easy, you can delete the sqlite file like the plists, don't forget to re initialize the persistent store, managed object context etc... after this operation

Upvotes: 1

Related Questions