cynistersix
cynistersix

Reputation: 1215

UIWebView caching not working for NSURLRequestReturnCacheDataElseLoad

I have setup an ARC compliant iOS 6 SDK built application to try to test out UIWebView and the caching of NSURLCache sharedCache.

I'm forming my request for the UIWebView like this:

-(void)runRequestWithMode:(NSURLRequestCachePolicy)policyMode
{
    NSString *theURL = @"http://server.mine.com:5555/test.htm";

    NSURL *url = [[NSURL alloc] initWithString:theURL];
    NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc] initWithURL:url
                                                                   cachePolicy:policyMode
                                                               timeoutInterval:30.0];

    if ([urlRequest cachePolicy] != policyMode) {
        NSLog(@"!!!!!!!!OVERRULED!!!!!!!");
    }

    [_webView loadRequest:urlRequest];
}

And inside the UIWebViewDelegate callback for should load:

    -  (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

//    NSLog(@"webView shouldStartLoadWithRequest loading %@", [[request URL] absoluteString]);

    NSLog(@"webView request cachePolicy: %d", [request cachePolicy]);

The console output of my logging shows the request is not using the cachePolicy I set on the mutable request by the time this callback is run.

Even if I manipulate the request at this point to set the cachePolicy to the correct initial setting, the server site is still not reporting 304 (data request is of an unchanged page and to use the cache in response)

From the desktop using chrome I do get a 304 response from the server versus the 200 I see on the device when I reload the page.

In my app delegate I've setup my url cache as such:

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                         diskCapacity:20 * 1024 * 1024
                                                             diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

What do I need to do to get my application's web view to receive a 304 response?

Any help or pointer where to read documentation would be appreciated.

Upvotes: 1

Views: 3317

Answers (1)

cynistersix
cynistersix

Reputation: 1215

Looks like UIWebView does not use NSURLCache...

How to cache content in UIWebView for faster loading later on?

Has a good insight to what is going wrong with the UIWebView on my end. I will try this and see if I can implement the final solution using it as my base to start from.

Upvotes: 1

Related Questions