Mike Rychev
Mike Rychev

Reputation: 271

How to clear UIWebView cache?

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

Upvotes: 27

Views: 61040

Answers (9)

Hamid-Ghasemi
Hamid-Ghasemi

Reputation: 294

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

Upvotes: 0

Hardik Thakkar
Hardik Thakkar

Reputation: 15951

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

Upvotes: 2

jgorozco
jgorozco

Reputation: 593

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

Upvotes: 10

Frank Schmitt
Frank Schmitt

Reputation: 25766

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

Upvotes: 4

Kumaresan P
Kumaresan P

Reputation: 127

Use following code:

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

Upvotes: 0

Caleb Shay
Caleb Shay

Reputation: 2551

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

Upvotes: 32

Naveen Shan
Naveen Shan

Reputation: 9192

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

Upvotes: 5

TomSwift
TomSwift

Reputation: 39512

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.

Upvotes: 11

Biranchi
Biranchi

Reputation: 16317

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

[webView loadRequest:request];

(now with typo fixed)

Upvotes: 17

Related Questions