3

I have an iOS app which stores data in NSUserDefults and many other data is set in cache as a result of web view load, social media signing etc. I want to remove all the data from cache created by the app.

Is there any way to do this programmatically in iOS?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Neenu
  • 6,848
  • 2
  • 28
  • 54
  • this link already covers your question: [link](http://stackoverflow.com/questions/10460690/can-i-programmatically-wipe-the-application-data-in-applicationdidfinishlaunchin) – Vi Matviichuk Nov 25 '14 at 09:43

6 Answers6

3

You could try this

NSUserDefaults

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

or

NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict) {
    [defs removeObjectForKey:key];
}
[defs synchronize];

UIWebview Cache

[NSURLCache sharedURLCache] removeAllCachedResponses];

//Delete cookies

 for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

  if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
  }
 }
Metalhead1247
  • 1,978
  • 1
  • 17
  • 28
2

Try

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

or

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];
1

Try this,

 [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"defunctPreference"];
1

Some of the caches are connected with other apps' information. You should be careful when you delete them. Just remind you to check before operating. I have made the similar stupid mistakes before. If you have found a great tool. Please share! Thanks.

0

You can try using [[NSURLCache sharedURLCache] removeAllCachedResponses];

For more details, refer to link http://iphonedevsdk.com/forum/iphone-sdk-development/104198-programmatically-clearing-app-cache.html

Vi Matviichuk
  • 1,122
  • 15
  • 32
  • what if you try this [link](http://stackoverflow.com/questions/15064854/delete-files-from-nscachesdirectory-programmatically) ? – Vi Matviichuk Nov 25 '14 at 10:01
0

If you use NSUserDefaults, you may try this.

In terms of other cache files, I think you should keep all of them in a folder, so that they can be removed easily by removing the whole cache folder.

Community
  • 1
  • 1
Henry H Miao
  • 3,420
  • 4
  • 20
  • 26