Thanks for the lead! It got me unstuck. Unfortunately, I seem to be running into a bunch of roadblocks.
The problem is that if I use NSURLRequestReturnCacheDataDontLoad, I think the resources that I'm loading must already be in the cache.
To put them in the cache, I tried this:
NSURLRequest *request = [NSURLRequest
requestWithURL:cachedURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0] ;
NSURLResponse *responseToCache =
[[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:@"text/html"
expectedContentLength:[dataAtURL length]
textEncodingName:nil] ;
NSCachedURLResponse *cachedResponse =
[[NSCachedURLResponse alloc]
initWithResponse:responseToCache data:dataAtURL
userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly] ;
// Store it
[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:request] ;
[responseToCache release] ;
[cachedResponse release] ;
NSLog( @"*** request %@, cache=%@", request ,[[NSURLCache sharedURLCache]
cachedResponseForRequest:request] ) ;
I got this code from elsewhere on the 'net, but I think that although it worked on the Mac, it isn't working on the iPhone, my target platform.
It doesn't seem to insert the item into the cache; the NSLog prints null for "cache=%@".
Then, I tried overloading NSURLCache:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSLog( @"FileFriendlyURLCache asked from request %lx of type %@ for data at URL: %@" ,
request , [request class] , [[request URL] absoluteString] ) ;
NSCachedURLResponse *result = nil ;
if( [[[request URL] absoluteString] hasPrefix:@"file://"] )
{
NSLog( @"\tFulfilling from cache" ) ;
NSError *error = nil ;
NSData *dataAtURL = [NSData dataWithContentsOfURL:[request URL]
options:0 error:&error] ;
if( error )
NSLog( @"FileFriendlyURLCache encountered an error while loading %@: %@" ,
[request URL] , error ) ;
NSURLResponse *reponse = [[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:@"text/html"
expectedContentLength:[dataAtURL length]
textEncodingName:nil] ;
result = [[NSCachedURLResponse alloc] initWithResponse:reponse data:dataAtURL
userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly] ;
#warning LEOPARD BUG MAKES IT SO I DONT AUTORELEASE result NSCachedURLResponse
[reponse release] ;
}
else
{
NSLog( @"\tFulfilling from web" ) ;
result = [super cachedResponseForRequest:request] ;
}
NSLog( @"Result = %@" , result ) ;
return result ;
}
In my request, I specify a cache policy of NSURLRequestReturnCacheDataDontLoad. This method seems to be called just fine, but it has strange behaviors on the iPhone. Regardless of whether or not I return an NSCachedURLResponse instance, the UIWebView still returns an error:
Error Domain=NSURLErrorDomain Code=-1008 UserInfo=0x45722a0 "resource unavailable"
It's as if the UIWebView ignores the fact that it's getting something other than nil back and failing anyway. I then got suspicious and wondered if the entire URL loading system simply just ignored everything coming from -cachedResponseForRequest, so I created a subclass of NSCachedURLResponse that looks like this:
@implementation DebugCachedURLResponse
- (NSData *)data
{
NSLog( @"**** DebugCachedURLResponse data accessed." ) ;
return [super data] ;
}
- (NSURLResponse *)response
{
NSLog( @"**** DebugCachedURLResponse response accessed." ) ;
return [super response] ;
}
- (NSURLCacheStoragePolicy)storagePolicy
{
NSLog( @"**** DebugCachedURLResponse storagePolicy accessed." ) ;
return [super storagePolicy] ;
}
- (NSDictionary *)userInfo
{
NSLog( @"**** DebugCachedURLResponse userInfo accessed." ) ;
return [super userInfo] ;
}
@end
I then modified -cachedResponseForRequest to use this class instead:
result = [[DebugCachedURLResponse alloc] initWithResponse:reponse data:dataAtURL
userInfo:nil storagePolicy:NSURLCacheStorageAllowedInMemoryOnly] ;
I set the request to have a NSURLRequestUseProtocolCachePolicy cache policy and ran the program. Although I can see my version of -cachedResponseForRequest being called and returning DebugCachedURLResponses for all file URLs, none of my debugging is being called, telling me that my DebugCachedURLResponse instances are being completely ignored!
So, here again, I'm stuck. I don't suppose you have any other ideas?
Many thanks for your first reply.