Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent a uiwebview from caching a web page?

I have a web view that loads a url on request. However, when this web view reloads the same url, the content does not get updated, but remains as it was after it was first loaded. I'm assuming this is due to automatic caching? Deleting the apps documents and data allows the webview to be refreshed, but then the issue occurs again.

like image 961
Jesse Head Avatar asked Jan 17 '12 01:01

Jesse Head


2 Answers

Try setting NSURLRequest's cachePolicy to NSURLRequestReloadIgnoringLocalAndRemoteCacheData.

like image 137
Fernando Valente Avatar answered Oct 12 '22 09:10

Fernando Valente


From NSURLRequest.h iOS 8.0

typedef NS_ENUM(NSUInteger, NSURLRequestCachePolicy)
{
    NSURLRequestUseProtocolCachePolicy = 0,

    NSURLRequestReloadIgnoringLocalCacheData = 1,
    NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
    NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

    NSURLRequestReturnCacheDataElseLoad = 2,
    NSURLRequestReturnCacheDataDontLoad = 3,

    NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};

you should be using NSURLRequestReloadIgnoringCacheData. I had the same problem. I relied on autocomplete and used the unimplemented methods.

like image 25
Pacu Avatar answered Oct 12 '22 09:10

Pacu