2

Im loading xhtml pages of a book in webview. when open a book and load its pages in webview for first time all works fine, but when i go back to home screen and load other books then the cover page and index page (cover.xhtml & index.xhtml ) for the book are show that of the old book (first book that was opened ). It's same for all books, i.e. cover & index page for any book is show as that of first opened book.

I tried many things like removing webview and adding back.

[self loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];   ,


[webView loadHTMLString:@"<html><head></head><body></body></html>" baseURL:nil];

none helped. The thing is its only giving problem with cover and index page rest of the pages are fine.

Is it because the name of the file is same (cover.xhtml) in all books so its cached and always give same content?. If so what is the solution.

Preetham
  • 125
  • 1
  • 11

4 Answers4

1

You could clear the cache when making that request using

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];

and if you want to clear the entire cache you can call

[[NSURLCache sharedURLCache] removeAllCachedResponses];
some_id
  • 29,466
  • 62
  • 182
  • 304
1

It is because it is loading the data from cache instead of loading it from the request URL.

There is a small workaround which i allways use when I don't trust the system and servers to make sure that my data is not cached

add a simple junk parameter at the end of the request with a random value everytime.

For example

instead of loading http://www.example.com/somepage.php

fetch data from http://www.example.com/somepage.php?junkValue=123456

It is not a perfect solution. perfect solution its check cache control headers in nsurlrequest object and to fetch data accordingly, or to disable caching for request object.

Vaibhav Gautam
  • 2,074
  • 17
  • 17
  • already tried by giving ?param=123 [random generated value ] as said in http://stackoverflow.com/questions/13886540/nsfilemanager-uploads-new-file-but-uiwebview-shows-cached-version?rq=1 – Preetham Aug 05 '13 at 08:00
  • Then you should change the caching policy. `[[NSURLCache sharedURLCache] removeAllCachedResponses]; ` use this before making calls to read data – Vaibhav Gautam Aug 05 '13 at 08:17
1

Try this, once you go back to your home screen add this line of code in your exit method

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];

I know its a poor hack but it cleans your webview.

Saify
  • 190
  • 10
1

Please Try to set Webview request`s caching Policy like below,

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

Have look apple CachePolicy for NSURLRequest here

AJPatel
  • 2,291
  • 23
  • 42