views:

555

answers:

1

I'm tinkering with adding a model layer to an iPhone application so that I can serialize/prioritize HTTP requests and selectively cache responses. Thanks to UIWebViewDelegate, the following method makes this fairly straight forward (in theory):

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

Basically, my code inspects the navigationType, sends the request off to the model, and returns NO. In turn, the model layer handles the request and, when complete, stuffs the data back into the UIWebView using:

- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)encodingName baseURL:(NSURL *)baseURL

Unfortunately, when pushing data back into the UIWebView, I often see shouldStartLoadWithRequest triggered again (this time with a navigationType of 5, but using the same URL as the original request). I cannot rely on the model to serve this from the cache (as the URL is identical to before), so I've got to have the UIWebView handle it itself by returning YES.

I'd like to avoid this such that the model (and caching layer) see and handle all requests. Does anyone have any ideas why I'm seeing a navigationType of 5 in a secondary shouldStartLoadWithRequest?

+2  A: 

I implemented an app using the exact same strategy that you're pursuing, and I wrestled with the same problem.

The navigationType you're seeing is UIWebViewNavigationTypeOther. In my application, I was seeing that navigationType on both my initial request, and on the request that resulted from my call to loadData:MIMEType:textEncodingName:baseURL. There was no way to tell the difference based on the parameters provided.

The solution gave me the shivers, but it was 100% reliable. In practice, every other time webView:ShouldStartLoadWithRequest:navigationType: got called, it was due to my call to loadData:MIMEType:textEncodingName:baseURL, so I solved this by creating a BOOL flag called something like loadingCachedData, and toggling it between YES and NO each time webView:ShouldStartLoadWithRequest:navigationType: got called. If the flag was YES, I'd let the request through. Otherwise, I'd dip into my cache. Worked like a charm.

cduhn
Yeah, that smacks of pure evil, but I really don't see many other options and, in the end, I did the same thing. Thanks!
D Carney
You should accept this as an answer, then! :)
Adam Woś