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

http://stackoverflow.com/questions/5468553/clearing-uiwebview-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

//init
- (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

http://ronglei0324.blog.163.com/blog/static/6763322320130910463392/

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.

https://github.com/rnapier/RNCachingURLProtocol

Nick Dowell's redirect:( Code to fix HTTP redirect handling: https://gist.github.com/1885821 )

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

http://blog.sina.com.cn/s/blog_6796844601010nx9.html

iOS replace UIWebView request with local file (NSURLCache)

http://blog.csdn.net/muyu114/article/details/7469514

Substituting local data for remote UIWebView requests
http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html

iphone UIWebView cache problem (apache http)

http://hi.baidu.com/marktian/item/668a7a4e763149ee1e19bcf4

UIWebView offline cache (NSURLCache)

http://yaozi20008.diandian.com/post/2012-03-21/18162236

how to use UIWebView cache

1.HTML5 , Manifest - UIWebView not fully support HTML5, only rendering of Webkit!
2.NSURLCache
3.ASIHTTPRequest,ASIDownloadCache 和 ASIWebPageRequest
http://www.cocoachina.com/bbs/read.php?tid=69287

Rating

rating: 0+x

Comment

Add a New Comment