iOS: Force UIWebView to reload / prevent caching

ios5, objective-c, uiwebview

Solution

This code snipplet fixed my problem:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Problem

I have an iPhone app that shows a website. This website changes it's content from time to time but my app doesn't reload it every time it is opened. I tried to prevent the caching of the site, but without success. This is my init method: ``` - (id)init:(NSString *)url withTitle:(NSString *)theTitle withPageName:(NSString *)pageName { self = [super init]; if(self) { NSLog(@"websiteviewcontroller init"); self.title = theTitle; self.url = url; self.pageName = pageName; CGRect frame = [[UIScreen mainScreen] applicationFrame]; webView = [[UIWebView alloc] initWithFrame:frame]; NSURL *theURL = [NSURL URLWithString:url]; [webView loadRequest:[NSURLRequest requestWithURL:theURL]]; // support zooming webView.scalesPageToFit = YES; //[[NSURLCache sharedURLCache] removeAllCachedResponses]; [webView reload]; } return self; } ``` This is my viewWillAppear-Method: ``` - (void)viewWillAppear:(BOOL)animated { CGRect frame = [[UIScreen mainScreen] applicationFrame]; webView = [[UIWebView alloc] initWithFrame:frame]; NSURL *theURL = [NSURL URLWithString:url]; [webView loadRequest:[NSURLRequest requestWithURL:theURL]]; [webView reload]; // support zooming webView.scalesPageToFit = YES; } ``` Please help me to solve this problem. All answers are much appreciated.

Original source

Related problems