4

I have an app that uses UIWebview which is used to browser different sites/urls. I am in need of an option "clear client data" similar to iOS mobile safari "Remove All website Data" that removes cookies/local storage. I know how to do the cookies but i am unable to figure out how to remove/clear the later (local storage)

How to remove HTML5 persistant databases in UIWebView? discussed this but i didn't see how that works for my above case. localStorage.clear() javascript needs to be run in UIWebview stringByEvaluatingJavaScriptFromString() method but this didn't seem to have worked in my testing. The other concern with the above approach is that, localStorage is url/site specific - but i want to clear all the contents inside localStorage.

Tried removing/copying Appln/Library/WebKit/LocalStorage folder - but this didnt work out with access restriction errors.

Any suggestions/directions please ?

Community
  • 1
  • 1
Naveen
  • 41
  • 1
  • 3

2 Answers2

8

You should be able to do it cleaning the content of the Caches directory of your app:

EDIT: Update code as suggested by @RaphaelSchweikert because of wrong error management.

// first, remove the cookies. 
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
    [storage deleteCookie:cookie];
}

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
for (NSString *string in array) {
    NSLog(@"Removing %@", [path stringByAppendingPathComponent:string]);
if ([[string pathExtension] isEqualToString:@"localstorage"])
    [[NSFileManager defaultManager] removeItemAtPath:[path stringByAppendingPathComponent:string] error:nil];
}

Inside that folder on my simulator I see many .localstorage files ;-) Try and let me know

LombaX
  • 17,265
  • 5
  • 52
  • 77
  • I can remove the files, but on application launch, there is a message in my NSLog saying: “Restored: localStorage database” and the localStorage contains the same values as always. – Raphael Schweikert Nov 04 '12 at 23:09
  • BTW: You should check the return value of these functions instead of checking the error flag because methods that use `NSError` are not guaranteed to actually set the pointer to `nil`. – Raphael Schweikert Nov 04 '12 at 23:44
  • Are you saying that, even without an error, the error pointer (that I've set to nil before calling the method) could be set to some value? Uh...I didn't know that. I'll search about. However my code was not blocking in case of error, only alerting ;-) – LombaX Nov 05 '12 at 07:09
  • Yeah, Apple says the following: „Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.“ (Source: https://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html). – Raphael Schweikert Nov 05 '12 at 08:26
  • This means they don’t guarantee that that the `NSError` object is null (or even points to any valid object – which would be bad when trying to send it a message such as `description`) on success, only that the opposite is true: it does point to a valid `NSError` object on failure. They usually don’t change a passed-in pointer on success bu they are free to change this behaviour at any time with later versions of the framework if they think it wise (for performance or other reasons). – Raphael Schweikert Nov 05 '12 at 08:30
  • Thank you, I've completely lost this part of the documentation :-) – LombaX Nov 05 '12 at 08:40
  • I've updated my answer. I leaved the cookie part, because cookies are stored in another directory (not the caches directory) and I think is a good idea to remove it, for this reason: if coded to do so, the website could remember you and restore a web backup of your localstorage data again :-) – LombaX Nov 05 '12 at 08:53
  • This solutions seems to work only after the app is closed and then relaunched. Am I right? Is there a workaround for that? – calvillo Dec 27 '14 at 01:52
  • Anybody else having huge problems with localStorage not getting deleted in the iOS simulator? I have asked a separate question about what I think is the problem: http://stackoverflow.com/q/30343373/1465640 – Pylinux May 27 '15 at 05:39
1

Note, that, if you’re using PhoneGap, you’ll also have to clear their internal backup of the localstorage cache:

//Remove the localstorage db
NSString *path = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Backups"] stringByAppendingPathComponent:@"localstorage.appdata.db"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

//Also remove the cached versions
path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
for (NSString *string in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]) {
    if ([[string pathExtension] isEqualToString:@"localstorage"]) {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    }
}
Raphael Schweikert
  • 18,244
  • 6
  • 55
  • 75
  • Is it a phonegap application? The cache backup system you describe seems to be the one I saw here! https://m.getsatisfaction.com/nitobi#/topics/phonegapbuild_localstorage_on_ios_5_1 it shouldn't be iOS stuff ;-) – LombaX Nov 05 '12 at 07:16
  • Ah, ok, now it all makes sense! I’ll adjust my answer. – Raphael Schweikert Nov 05 '12 at 08:19