problem with custom NSProtocol and caching on iPhone

Posted by TomSwift on Stack Overflow See other posts from Stack Overflow or by TomSwift
Published on 2010-03-11T18:55:23Z Indexed on 2010/03/11 18:59 UTC
Read the original article Hit count: 317

My iPhone app embeds a UIWebView which loads html via a custom NSProtocol handler I have registered.

My problem is that resources referenced in the returned html, which are also loaded via my custom protocol handler, are cached and never reloaded. In particular, my stylesheet is cached:

    <link rel="stylesheet" type="text/css" href="./styles.css" /> 

The initial request to load the html in the UIWebView looks like this:

 NSString* strUrl = [NSMutableString stringWithFormat: @"myprotocol:///entry?id=%d", entryID ];

NSURL* url = [NSURL URLWithString: strUrl]; [_pCurrentWebView loadRequest: [NSURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 60 ]];

(note the cache policy is set to ignore, and I've verified this cache policy carries through to subsequent requests for page resources on the initial load)

The protocol handler loads the html from a database and returns it to the client using code like this:

 // create the response record

NSURLResponse *response = [[NSURLResponse alloc] initWithURL: [request URL] MIMEType: mimeType expectedContentLength: -1 textEncodingName: textEncodingName];

// get a reference to the client so we can hand off the data id client = [self client];

// turn off caching for this response data [client URLProtocol: self didReceiveResponse:response cacheStoragePolicy: NSURLCacheStorageNotAllowed];

// set the data in the response to our jfif data
[client URLProtocol: self didLoadData:data]; [data release];

(Note the response cache policy is "not allowed").

Any ideas how I can make it NOT cache my styles.css resource? I need to be able to dynamically alter the content of this resource on subsequent loads of html that references this file.

I thought clearing the shared url cache would work, but it doesnt:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

One thing that does work, but it's terribly inefficient, is to dynamically cache-bust the url for the stylesheet by adding a timestamp parameter:

<link rel="stylesheet" type="text/css" href="./styles.css?ts=1234567890" /> 

To make this work I have to load my html from the db, search and replace the url for the stylesheet with a cache-busting parameter that changes on each request. I'd rather not do this.

My presumption is that there is no problem if I were to load my content via the built-in HTTP protocol. In that case, I'm guessing that the UIWebView looks at any Cache-Control flags in the NSURLHTTPResponse object's http headers and abides by them. Since my NSURLResponseObject has no http headers (it's not http...) then perhaps UIWebView just decides to cached the resource (ignoring the NSURLRequest caching directive?).

Ideas???

© Stack Overflow or respective owner

Related posts about iphone

Related posts about nsurlprotocol