iOS - About UIWebView Cache

Blog » iOS - About UIWebView Cache

Post at 15 Aug 2013 09:25

iOS - About UIWebView Cache, there are lots of discussion

Major topic are:

  • Cache all request, for offline browsing or replace
  • Cache all request, and flush cache when need
  • Controll the size and performance

stackoverflow clear cache

Code 1

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];

Code 2

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
    [NSURLCache setSharedURLCache:sharedCache];
    // ... other launching code
//purge the cache (for example in applicationDidReceiveMemoryWarning or when you close a UIWebView) 
[[NSURLCache sharedURLCache] removeAllCachedResponses];

Code 3

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

UIWebView Offline cache with NSURLProtocol

Rob Napier's blog "Drop-in offline caching for UIWebView (and NSURLProtocol)" show us a way of UIWebView Offline cache with NSURLProtocol. the source can be downloaded from github.

Nick Dowell's redirect:( Code to fix HTTP redirect handling: )

(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { 
if ([response isKindOfClass:[NSHTTPURLResponse class]]) 
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
 if ([HTTPResponse statusCode] == 301 || [HTTPResponse statusCode] == 302) 
NSMutableURLRequest *mutableRequest = [request mutableCopy]; 
[mutableRequest setURL:[NSURL URLWithString:[[HTTPResponse allHeaderFields] objectForKey:@”Location]]];
 request = [mutableRequest copy];
 [[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response]; 
return request; 

Other reference

uiwebview cache (NSURLCache)

iOS replace UIWebView request with local file (NSURLCache)

Substituting local data for remote UIWebView requests

iphone UIWebView cache problem (apache http)

UIWebView offline cache (NSURLCache)

how to use UIWebView cache

1.HTML5 , Manifest - UIWebView not fully support HTML5, only rendering of Webkit!
3.ASIHTTPRequest,ASIDownloadCache 和 ASIWebPageRequest


rating: 0+x


Add a New Comment