How to clear UIWebView cache?

霸气de小男生 提交于 2019-12-17 06:39:23

问题


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?

Thanks in advance


回答1:


// Flush all cached data
[[NSURLCache sharedURLCache] removeAllCachedResponses];



回答2:


NSURLRequest* request = [NSURLRequest requestWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];

[webView loadRequest:request];

(now with typo fixed)




回答3:


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.




回答4:


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).




回答5:


//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.




回答6:


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).




回答7:


For swift 3

//to remove cache from UIWebview
URLCache.shared.removeAllCachedResponses()
    if let cookies = HTTPCookieStorage.shared.cookies {
        for cookie in cookies {
            HTTPCookieStorage.shared.deleteCookie(cookie)
        }
    }



回答8:


Use following code:

NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
  [[NSFileManager defaultManager]removeItemAtPath:cacheDir error:nil];



回答9:


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];
}


来源:https://stackoverflow.com/questions/2523435/how-to-clear-uiwebview-cache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!