iOS Retrieve cache in webview - ios

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

Related

Download progress is not display in NSProgress when try download file from GoDaddy server url

I want to donwload mp3 file from goDaddy server and display donwload process in NSPorgress.
That's why I'm using Heikowi/HWIFileDownload this demo to refrence.
Demo is working fine with start, stop,cancel,pause and resume functionality.
But in this demo I can't able to download url from GoDaddy server.
How can I solve this?
I'm using this below url:
http://starindustries.co/assets/mp3/Exotic.mp3
I just found the solution that make your user class you just need to change following code in Download.m find start Method
Replace following existing line of code:
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
With:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url];
[request setValue:#"identity" forHTTPHeaderField:#"Accept-Encoding"];
That's it your issue going to be solve. you getting actual length instead of -1

Remove UIWebView's internal cache

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.

NSURLRequest: Always getting NSCachedURLResponse back.

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.

Loadrequest: in a Non-Visible UIWebView or load a webPage without using a UIWebView?

Am need to download and cache an entire webPage before navigating to another screen. For caching I have sub classed NSURLCache and and saving the response by overriding cachedResponseForRequest:request and storeCachedResponse:forRequest: methods. It works and caches without any problems when There is webView and I load the request in it.
In my case I need to download many webPages before its actually displayed on a webView. So how do I simulate the loadRequest: of a UIWebView without actually adding it as a subView anywhere? or preferably any other way I can load a webpage without using a UIWebView at all..??
Here is what I discovered :
If the UIwebView is not added as subView, and if I call loadRequest:, the request doesn't load (not even delegate methods are called..!)
I tried with NSURLConnection,
[NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
}];
and also,
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlrequest delegate:nil];
if (theConnection) {
}
But the problem with the above approach is that, it only sends only one request, i mean it won't request for images,css and js files in the webPage which I need to cache.
So, what's the way to make it request everything just like in UIWebView's loadRequest..??

how to get NSURLRequest in UIWebViewDelegate#didFailLoadWithError?

How can we retrieve the NSURLRequest that failed to load when the UIWebViewDelegate is called back with didFailLoadWithError (which gets only the NSError) ? We could stash the NSURLRequest when shouldStartLoadWithRequest is called, but what if the UIWebView is loading multiple requests simultaneously (e.g., a page with scripts, stylesheets, etc.) How do we determine which URL failed to load?
Hmm... looks like the NSError has a NSErrorFailingURLKey key with the URL which is good enough for now... but is there a way to directly get the NSURLRequest?
Can you get it from the webView? webView.request?

Resources