1
votes

I'm building an iOS app using Cordova/Phonegap that basically has a UIWebView and loads my web application from external resource. (iOS6+)

This web app needs a 450kb HTML to run, and I want to have this file cached when there is no changes to it. (Changes every other week)

For that, I'm setting up a NSURLCache in my AppDelegate during initialization:

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

After app is loaded, files are now caching, and I can find them on Library/Caches/APPName/fsCachedData.

I'm using CharlesProxy to see the requests, and if the App is loaded once, all the reloads return the 304, Not Modified, to that file

Here is the Problem

Every time I open the iOS App, although the file is in the cache folder, it loads it again from the server and saves the most recent version; If-modified-since is not set to the request parameter in the first request after opening, but is the following ones.

I'm not sure if I need to explicitly ask Xcode to load NSURLCache from disk or I'm not setting it properly.

Can anyone see a problem in the process?

More Info:

Cache Control is private, max-age=31536000, must-revalidate. NSURLRequest cachePolicy is NSURLRequestUseProtocolCachePolicy.

Thanks!

1

1 Answers

1
votes

The problem was caused by CordovaLibrary. It has a CDVURLProtocol & CDVUserAgent and they intercept requests, conflicting with existing information.

To fix the problem you have to prevent CDVUserAgent from setting UserAgent, but it may affect restrictions to local files. In my case, sources are retrieved from server.