I'm showing a web app in an UIWebView, and sometimes the content of pages will change. After content have been changed the app clears the cache. But when I go to a page I've previously visited the UIWebView doesn't send a HTTP GET request, but loads from cache even though I've disabled cache like so:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
Initally I'm loading a request with cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];
UIWebView have some kind of internal cache. Already visited pages will be loaded from this internal cache instead of going through NSURLCache and also there's no request sent.
Is there any way to clear the internal cache of UIWebView? I'm even recreating the UIWebView but the cache is still there.
It appears that what's happening here is that it reloads the actual HTML file, but does not necessarily reload the resources within that page.
A possible solution I've seen is to append a query parameter on to the end of the URL. For example:
NSString *testURL = [NSString stringWithFormat:#"%#?t=%#", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
where you generate a random alphanumeric string as your randQuery query parameter, or keep a persistent count and just count up.
This should force the UIWebView to load from the remote resource.
I had the same issue and setting HTTPShouldHandleCookies property to NO fixed my problem.
For example:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strurl]];
[request setHTTPShouldHandleCookies:NO];
[webView loadRequest: request];
Hope this help.
Related
I'm using code that I got from a tutorial for finding the "Date Modified" info of a file on a server. It goes as follows...
// create a HTTP request to get the file information from the web server
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:remoteFileURL];
[request setHTTPMethod:#"HEAD"];
NSHTTPURLResponse* response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// get the last modified info from the HTTP header
NSString* httpLastModified = nil;
if ([response respondsToSelector:#selector(allHeaderFields)]) {
httpLastModified = [[response allHeaderFields] objectForKey:#"Last-Modified"];
}
And, for the most part, it works! Wa-hoo.
Except, for a few files, it seems to be returning outdated information. In fact, for at least one, it's returning a date (Fri, 24 Apr 2015 04:32:55 GMT) for a file that doesn't even exist anymore. I deleted it from the server, but it's still returning that value every time I run my app, as if the file still existed.
I've checked and re-checked that the remoteFileURL is pointing to the right URL, and I've confirmed that the file doesn't exist on the server anymore. And, like I said, for most files it works perfectly, so obviously the system isn't entirely broken.
This is my first experience with NSMutableURLRequest and NSHTTPURLResponse and getting file headers and such. Is there something I don't know, that would explain this? Could there be a cache or something that needs to be cleared?
And ideally, what can I do to ensure that the proper info is being received by my app?
Thanks in advance!
Posting my comment as an answer
This type of thing happends because of caching mechanism of NSURL.
Try to add below line to remove all cached data.
[[NSURLCache sharedURLCache] removeAllCachedResponses];
If you want to ignore caching for particulate request try below code
NSURL *url = [NSURL URLWithString:aStrFinalURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
I am using ASIHttpRequest and make use GET method to send header to server. I call method addRequestHeader like this
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"myHeader" value:#"abc"];
It's not working. But if I use NSMutableURLRequest to add header and request to server, it works.
I don't know anything wrong when calling addRequestHeader methods for ASIHTTPRequest library.
Have anyone seen this issue?
Wow ok so, yeah if this is a new app, please do NOT use ASIHttpRequest. It has long been supplanted by the delightful AFNetworking.
If this is an existing application, you really should work on a migration plan off ASI.
However, in an attempt to actually answer your question - that is the appropriate setup per the documentation, and is how I used to use it. My guess is something is broken under the covers and judging from a basic google request, there are issues with iOS 7 including memory leaks and requests just failing.
You can do it via NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.abc.com"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:#"AAA" forHTTPHeaderField:#"Hello-there"];
request = [mutableRequest copy];
NSLog(#"%#", request.allHTTPHeaderFields);
Hope this helps .. :)
I'm using AfNetworking, I make a call for new data but I keep getting the cached result back. So if I'm in a VC that's shows the data, pop back to the root and change data on my server, I then wait 30sec and when I push back into the VC I'll see the the old data. If I hit the URL in a browser I see the correct data. If I re-run the app I will see the changes to the data.
My response from my server sends back cache control header: Cache-Control:max-age=10, public
From what I can tell is that I always always get a NSCachedURLResponse back and that the cache is not listening to my cache-controll policy.
In my AppDelegate I set my SharedURLCache:
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:8 * 1024 * 1024 diskCapacity:8 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
How I set the URLRequest:
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
I do nothing else involving NSURLCache.
Any ideas?
BooRanger
The behavior you describe is exactly what I'd expect from the code you've posted. Have you tried using NSURLRequestReloadIgnoringLocalAndRemoteCacheData instead of NSURLRequestUseProtocolCachePolicy? According to this NSURLRequestUseProtocolCachePolicy means:
Specifies that the caching logic defined in the protocol
implementation, if any, is used for a particular URL load request.
This is the default policy for URL load requests.
So if you want it to always load from the server, you should use NSURLRequestReloadIgnoringLocalAndRemoteCacheData which is described as:
Specifies that not only should the local cache data be ignored, but
that proxies and other intermediates should be instructed to disregard
their caches so far as the protocol allows.
I'm using ASIHTTPRequest with caching for downloading pictures in my iOS app. Most pictures never change and hence can leverage the caching functionality. There are a few that do change based on a certain function in the iOS app. So, I know exactly when these images will change. How can I clear the cache of a certain link (e.g. http://www.test.com/image.jpg) and preserve the cache for all other requests in my app. Thanks.
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:#"{Image url}"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setSecondsToCache:60*60*24*7];
[[ASIDownloadCache sharedCache] removeCachedDataForURL:[NSURL URLWithString:#"{Image url}"]];
I have a webview to show web content, let said google.com. I use
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[storeWebView loadRequest:request];
on the viewDidLoad function. BUT when I click another button, which is in the web page (not the UIButton), how to I 'save' the cache and when I click another button, or back button, it will find and retrieve the cache?
My understanding is the system will cache the page for you, and if you use same request again, it will first look in the system cache, and if the page is there it will load it up from cache...
to clear cache, use [[NSURLCache sharedURLCache] removeAllCachedResponses];
if you want more control, take a look at NSURLCache Class Reference