Jtaylorapps
Jtaylorapps

Reputation: 5770

How do you clear a UIWebView's cache?

Essentially, I need to know how to make a UIWebView erase everything when you terminate the app. Basically, that includes all cookies and information held by the webView through usage.

Is there any way to simply clear it so that it's like new each time?

Upvotes: 6

Views: 10000

Answers (1)

Jano
Jano

Reputation: 63667

UIWebView doesn't cache, it's the NSURLConnection it uses. Any of the following used to work, and follows the documentation of NSURLCache:

// remove all cached responses
[[NSURLCache sharedURLCache] removeAllCachedResponses];

// set an empty cache
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

// remove the cache for a particular request
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];

But I'm not sure if it does now (iOS 5). Other options are using a custom cache: SDURLCache or overriding connection:withCacheResponse:. If you find out what is the current behavior please comment.

Upvotes: 6

Related Questions