How to clear UIWebView cache?

前端 未结 9 1730
闹比i
闹比i 2020-11-29 04:47

I need UIWebView to display some local .webarchive file. But images there have same names, so UIWebView shows only one image all the time. How can I clear the cache?

相关标签:
9条回答
  • 2020-11-29 05:03

    I've had this issue myself with script and css files being cached.

    My solution was to put a cache-busting parameter on the src url:

    <script src='myurl?t=1234'> ...
    

    where t=1234 changes each time the page is loaded.

    0 讨论(0)
  • 2020-11-29 05:05

    I'm working with some designers editing the CSS files used by some web views in an app. They were complaining that changes to the stylesheets weren't being reflected in the app, and a bit of debugging with Charles confirmed that they weren't being reloaded. I tried seemingly every answer on StackOverflow, to no avail.

    What finally did the trick was creating an NSURLCache subclass that overrides -cachedResponseForRequest:

    - (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest*)request
    {
        if ([[[[request URL] absoluteString] pathExtension] caseInsensitiveCompare:@"css"] == NSOrderedSame)
            return nil;
        else
            return [super cachedResponseForRequest:request];
    }
    

    I then install it with a reasonable memory and disk capacity:

    NSURLCache *currentCache = [NSURLCache sharedURLCache];
    NSString *cachePath = [cachesDirectory() stringByAppendingPathComponent:@"DebugCache"];
    
    DebugURLCache *cache = [[DebugURLCache alloc] initWithMemoryCapacity:currentCache.memoryCapacity diskCapacity:currentCache.diskCapacity diskPath:cachePath];
    [NSURLCache setSharedURLCache:cache];
    [cache release];
    

    (cachesDirectory() is a function that returns /Library/Caches under the application directory on iOS).

    As you can tell by the name, I'm using this only in the debug configuration (using the Additional Preprocessor Flags build setting and some #ifdefs).

    0 讨论(0)
  • 2020-11-29 05:08

    this work like a charm !

    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    
    //Clear All Cookies
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    
    0 讨论(0)
  • Using

    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    

    BEFORE make the call or when you load the view controller, this is an static function that remove all cache data for responses. Only using cachePolicy it was not enought, i see that uiwebview could refresh the html document but not the linked document like CSS, JS or images.

    I try this solution and it works.

     if (lastReq){
        [[NSURLCache sharedURLCache] removeCachedResponseForRequest:lastReq];
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    }
    
    lastReq=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:localUrl]
                                       cachePolicy:NSURLRequestReloadIgnoringCacheData
                                   timeoutInterval:10000];
    [lastReq setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    
    [self.mainWebView loadRequest:lastReq];
    

    --First of all, i remove the cache for the last request and call to remove all other cache ( i think this is not so important) --Then i create a nsurlrequest and ignoringLocalCacheData --finally, load the new request

    I check it and it reload all files (html, css, js and images).

    0 讨论(0)
  • 2020-11-29 05:09

    Use following code:

    NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
      [[NSFileManager defaultManager]removeItemAtPath:cacheDir error:nil];
    
    0 讨论(0)
  • 2020-11-29 05:25
    //to prevent internal caching of webpages in application
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [sharedCache release];
    sharedCache = nil;
    

    try using this. It will clear the url cache memory of your application.

    0 讨论(0)
提交回复
热议问题