I need to show a UIWebView that opens a local html, fetching from a remote server. Each time, the UIWebView shows incorrect data, because the iOS caching system.
I did many attempts to solve this problem, but it does not work:
1) Programmatically add UIWebView when the UIViewController starts loading. After the UIViewController disappears, stop loading the UIWebView, and release the UIWebView.
2) Ignore the local cache data:
NSURL *websiteUrl = [NSURL fileURLWithPath:localHtmlPath]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:websiteUrl]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [self.webView loadRequest:request];
3) Clear the cache when the UIViewController disappears
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:localHtmlPath]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
4) Disable the cache
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
}
5) Reduce iOS memory utilization by taming NSURLCache
follow the tutorial link
- (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"];
[NSURLCache setSharedURLCache:sharedCache];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
6) Follow this link to prevent CSS caching.
None of these above methods works. Any workaround?