1

I'm working on an application which loads locally stored webpages through UIWebViews. After the user logs out, I'd like to remove all of the webpages' local data, such as cookies and HTML5 databases. I found this solution and it works:

how to remove html5 local storage of an iOS app using UIWebview

However, the removal of the local data is not reflected until the application is closed and then relaunched, it's like the information is loaded to RAM only when the app is launched. This is my code:

// Remove cookies
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
  [cookieStorage deleteCookie:cookie];
}

// Remove HTML local storage
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *fileArray = [fileManager contentsOfDirectoryAtPath:path error:nil];
for (NSString *file in fileArray) {
  if ([file.pathExtension isEqualToString:@"localstorage"])
    [fileManager removeItemAtPath:[path stringByAppendingPathComponent:file] error:nil];
}

I also added this line hoping it would fix the problem, it didn't work:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
Community
  • 1
  • 1
calvillo
  • 892
  • 1
  • 9
  • 23
  • Do you release the `UIWebView`? – zaph Dec 27 '14 at 02:01
  • I'm still understanding ARC, but I believe I do. I set the web view and its delegate to nil on the view controller's `viewDidDisappear`. Is that the right way to do it? – calvillo Dec 27 '14 at 02:23
  • Does `viewDidDisappear` get called? I am wondering how you know the objects are not being deallocated and why you care. – zaph Dec 27 '14 at 02:29
  • Yes, it gets called. I don't have that much experience with Objective-C and ARC, but I've programmed a lot in Perl which has its own automatic garbage collection too. I believe ARC should work in the same way (data remains in memory while there's at least one pointer to it), but maybe I'm wrong. – calvillo Dec 27 '14 at 02:41
  • Basically yes. Setting the pointer to nil release the memory if that is the only/last strong pointer but it does not clear the memory, the memory will remain intact until it is re-used. If there is also a weak pointer to it accessing by that pointer before the memory is re-used it will see the released memory and can access the object it pointed to. That is why I asked how you knew the memory was not released. – zaph Dec 27 '14 at 02:51
  • Oh, I understand a lot of things now, but the question is not about memory releasing. If I clear the files inside the Caches directory and then reload one of the webpages, its like the cache was not cleared at all. I have to manually close the app and relaunch it so the cache clearing actually works. The cache clearing code happens when the user logs out, which is after the view controller that contains the web view has already disappeared. – calvillo Dec 27 '14 at 02:59
  • @calvillo How did you fix that ? I got the same problem and don't know how to clean the cache actually !! – Tai Dao Feb 18 '16 at 10:49
  • 1
    @Dolphin I used to store my content into a directory with the same name every time. I ended up changing the name of the directory to a random one every time I updated its contents (using UUID's). That way, the cache was never an issue. – calvillo Feb 18 '16 at 19:33
  • @calvillo thanks you, I've researched a solution that quite similar to yours: http://stackoverflow.com/questions/19113682/remove-uiwebviews-internal-cache. So problem is that we can't clear the cache completely by using code. May be It's UIWebView bugs? :( – Tai Dao Feb 19 '16 at 02:54

0 Answers0