Skip to content

Instantly share code, notes, and snippets.

@username0x0a
Last active November 22, 2020 18:12
Show Gist options
  • Save username0x0a/67adfa0142767575194f to your computer and use it in GitHub Desktop.
Save username0x0a/67adfa0142767575194f to your computer and use it in GitHub Desktop.
App Delegate method allowing you to wipe all app data on demand
#define NSDocumentsDirectory() [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
#define NSLibraryDirectory() [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject]
#define NSCachesDirectory() [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
//efine NSTemporaryDirectory() Not needed - defined in Foundation
@implementation AppDelegate
- (void)performDataReset
{
// Clean-up all data folders...
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *folders = @[ NSCachesDirectory(), NSDocumentsDirectory(), NSTemporaryDirectory(), NSLibraryDirectory() ];
// ...except for some including Crashlytics data
NSArray *skipped = @[ @"com.crashlytics.data", @"Application Support", @"Caches" ];
// in Caches in Library in Library
NSError *error = nil;
for (NSString *folder in folders)
for (NSString *file in [fm contentsOfDirectoryAtPath:folder error:&error])
if (![skipped containsObject:file])
[fm removeItemAtPath:[folder stringByAppendingPathComponent:file] error:&error];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// Example usage:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Reset application data when selected
if ([[NSUserDefaults standardUserDefaults] boolForKey:kSettingsResetAppData])
[self performDataReset];
// ...
}
@end
@username0x0a
Copy link
Author

This peace of code allows to you purge data of your app on launch (filesystem data as well as user defaults).

To clear Crashlytics data as well, simply remove the skipped array and its checking inside for-loop.

Works smoothly on iOS 7+, including both the Simulator and devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment