user2538944
user2538944

Reputation: 305

UIwebview does not clear cache in iOS 7

I am working on an app where few HTML,CSS files are downloaded from the server and loaded in the webview like this

  [[NSURLCache sharedURLCache] removeAllCachedResponses];
  NSURL *url = [NSURL fileURLWithPath:htmlfile];
    htmlLoadRequest = [NSURLRequest requestWithURL:url];
    [self.objwebview loadRequest:htmlLoadRequest];

In the webview delegate method i have this piece of code

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ 

if(request.cachePolicy != NSURLRequestReloadIgnoringLocalCacheData) {
    NSURLRequest* noCacheRequest = [[NSURLRequest alloc] initWithURL:request.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:request.timeoutInterval];
    [webView loadRequest:noCacheRequest];

} // further code to do other stuff

In the viewWillDisappear method i have this piece of code

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];
[self.objwebview stopLoading];
[self.objwebview setDelegate:nil];

if (htmlLoadRequest) {
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:htmlLoadRequest];
}
[[NSURLCache sharedURLCache] removeAllCachedResponses];

NSString *htmlfile = [HTML_SERVER_FILES stringByAppendingPathComponent:@"CoverPage.html"];

self.objwebview = nil;

}

The place where i face the problem is that when i download the files for the first time then everything works fine but when i update few of my html files and then download them from the server then in that case what happens is i still see the first html files and their images.

This means that somewhere the webview is still maintaining the cache info for the first set that was downloaded and is not clearing it.

Also when my viewcontroller loads in the view will appear method i call this code

  NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;

but nothing happens still i see the same old HTML files which where downloaded from the server at the first place.

The only way to see the updated set is to kill the app and then launch it again which i find should not be done as it creates a bad user exp. So please help me out on how to resolve this issue.

Update : I went through this URL where it says that its a bug in iOS 7

Upvotes: 0

Views: 2907

Answers (3)

Lucman Abdulrachman
Lucman Abdulrachman

Reputation: 393

After removeAllCachedReponses and cookies deletion, removing all .db files under {Bundle Identifier} folder works for me.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // clear cache
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    // clear cookies
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        NSLog(@"CLEAR DOMAIN:%@", [cookie domain]);
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    // remove all db files since removeAllCachedResponses does not work all the time.
    [self removeCacheFiles];

    return YES;
}
-(void) removeCacheFiles
{
    NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *nsurlDir = [[NSString alloc] initWithFormat:@"%@/com.company.SampleApp", cacheDir];
    NSFileManager  *manager = [NSFileManager defaultManager];


    // grab all the files in the documents dir
    NSArray *allFiles = [manager contentsOfDirectoryAtPath:nsurlDir error:nil];

    // filter the array for only sqlite files
    NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.db'"];
    NSArray *dbFiles = [allFiles filteredArrayUsingPredicate:fltr];

    // use fast enumeration to iterate the array and delete the files
    for (NSString *dbFile in dbFiles)
    {
        NSError *error = nil;
        [manager removeItemAtPath:[nsurlDir stringByAppendingPathComponent:dbFile] error:&error];
        if (error != nil) {
            NSLog(@"Error:%@", [error description]);
        } else {
            NSLog(@"DB FILE Cleared: %@", dbFile);
        }
    }
    NSError *error = nil;
    [manager removeItemAtPath:[nsurlDir stringByAppendingString:@"/nsurlcache"] error:&error];
    if (error != nil) {
        NSLog(@"Error:%@", [error description]);
    } else {
        NSLog(@"Dir Cleared");
    }
}

Upvotes: 0

Bug Hunter Zoro
Bug Hunter Zoro

Reputation: 1915

The UIWebview may be maintaining a path history and will be loading items from it. What you need to do is change the name of the folder to some GUID every time you sync the app in this way the webview will have a new path to load its content.

Upvotes: 1

user2538944
user2538944

Reputation: 305

My observation was that UIwebview caches the entire path from where it displays the files and even after removing those files from the doc directory it seems to me that the webview maintains few reference to the old set.

So what i did was i changed the name of the folder in which i was saving the resource files to some GUID due to which every time i download resources set from the server a new fileURL was provided (your doc directory / someGUID) due to which the webview could load the new resource set downloaded from the server, so i did this

  • You download your resources in a folder from the server.

  • Every time you download the resource the name of the folder in which you download the files must be some GUID, you may use NSUUID class for that purpose (Ofcourse not to mention you need to store that UUID in some user defaults or plist so that you can use it further to give the entire path of your file)

And after implementing the above logic my app worked great.

Upvotes: 0

Related Questions