Last active
November 22, 2020 18:12
-
-
Save username0x0a/67adfa0142767575194f to your computer and use it in GitHub Desktop.
App Delegate method allowing you to wipe all app data on demand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.