Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Force UIWebView to reload / prevent caching

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.

like image 859
nimrod Avatar asked Jun 09 '12 15:06

nimrod


1 Answers

This code snipplet fixed my problem:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
like image 136
nimrod Avatar answered Oct 07 '22 16:10

nimrod