How clear UIWebView cache

2019-04-13 18:00发布

问题:

How to clear the cache of a UIWebView?

I am making a simple application (with only three buttons (back, forward, refresh) and a UIWebView) and I realized that in my Documents & Data iPhone gets a very high value of Cache. I already looked at various documents and posts (even here at StackOverflow) but none solved the problem. If you need lines of codes, ask.

回答1:

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];

This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

After that, you can try deleting any associated cookies with the UIWebView:

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}


回答2:

its so simple

[[NSURLCache sharedURLCache] removeAllCachedResponses];