5

I am trying to load UIWebView Offline with data from NSURLCache.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // setup cache
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB

    NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:urlCache];
}

-(void)loadWebView{
    request = [NSURLRequest requestWithURL:myURL cachePolicy: NSURLRequestReturnCacheDataElseLoad timeoutInterval:1000];
    [self.webView loadRequest:request];
}

When I try to load UIWebView offline it calls didFailWithError instead of connectionDidFinishLoading. My Cache directory however contains offline data and resources like css, js files but UIWebView is not able to access it.

I have confirmed this by overriding the following method:

-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
    NSCachedURLResponse *response = [super cachedResponseForRequest:request];

    if([response.data length]> 0){
       // this is called for all css , js script files
       NSLog(@"Returning data from cache");
    }
    return response;
}

Moreover, the data is being returned from cache UIWebview fails to load it.

Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
  • 1
    Check this answer: http://stackoverflow.com/a/15253094/1524700. You may want to modify `NSURLRequest` before you create a`NSCachedURLResponse`. – Andrey Apr 01 '14 at 21:44
  • @Andrey I have tried this and it did not worked didFailWithError is called before cachedResponseForRequest. So any modification here will not affect the connection delegate callbacks. – Kunal Balani Apr 01 '14 at 21:56
  • Wait, your question implies that `cachedResponseForRequest:` is called (you've been able to override it and `NSLog(@"Returning data from cache");` is called for certain requests). Is that correct? If so, then I suggest that you further modify `cachedResponseForRequest:` to incorporate changes in the answer I've linked to (mainly setting response code to `200`). – Andrey Apr 01 '14 at 22:12
  • I did , it didn't worked. It calls didFailWithError after that. – Kunal Balani Apr 02 '14 at 00:00
  • @Andrey Indeed . That was correct . After minor changes in headers it worked fine. Thank you so much . – Kunal Balani Apr 02 '14 at 15:26
  • What minor changes were these? I'm having problems getting UIWebView to access cache but as noted here cachedResponseForRequest *is* returning cached data. This is occurring on iOS6 not on iOS67. – Chris Prince Apr 25 '14 at 19:11
  • @ChrisPrince I was having incorrect policy for request object. [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad]; [webView loadRequest:request]; – Kunal Balani Apr 25 '14 at 19:13
  • Unfortunately, that's not working for me. On iOS6, when offline, having previously cached the data, NSURLRequestReturnCacheDataElseLoad and NSURLRequestReturnCacheDataDontLoad do not work for me. Again, cachedResponseForRequest is returning cached data, but this is not being utilized by the UIWebView. You mentioned that "After minor changes in headers it worked fine". Was it the header changes or the cache policy that helped you? – Chris Prince Apr 25 '14 at 19:24
  • @ChrisPrince thats what did it for me. – Kunal Balani Apr 25 '14 at 19:37

0 Answers0