I have a UIWebView when using the NSURLRequest I’m setting cachePolicy: NSURLRequestReturnCacheDataElseLoad and also setting the pro cache memory size. When I open the application with internet, I open the screens and then deactivate the internet, the url I open is cached, until then everything is fine, but when closing the application and open again it loses all the cache. Can you persist the cache?
I have this:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:500*1024*1024 diskCapacity:500*1024*1024 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
and loading from UIWebView:
NSURL *websiteURL = [NSURL URLWithString:URLString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
[self.mainWebView loadRequest:urlRequest];
I was able to resolve using a cache.manifest file on the server.
Related
I have walked through various solution to clear the local image cache in UIWebView, while i trying to load the image in html it atomically displays the previous image(i using the same name to replace the image in template).
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL fileURLWithPath:[[CustomFunctions getFilesPath] stringByAppendingPathComponent:obj]] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
[webview1 loadRequest:request];
[webview1 reload];
this is my code and can any one please suggest me to do this. Thanks in advance. sorry for my English.
//to prevent internal caching of webpages in application
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
//[sharedCache release];
sharedCache = nil;
try using this. It will clear the url cache memory of your application.
Try this ...
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
This would remove a cached response for a specific request. There is also a call that will remove all cached responses for all requests ran on the UIWebView:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
I ran this code because I think I am running into a caching issue:
NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if(response){
NSLog(#"Got Response");
}else{
NSLog(#"No Response");
}
my question is how do I clear cache for the request URL ?
NSURLRequest has a cachePolicy property, which specifies the caching behaviour of the request.
Set the following cache policy NSURLRequestReloadIgnoringLocalCacheData when making the request like the example bellow will load the data from the url and not from the cache.
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
NSURLRequestReloadIgnoringLocalCacheData
Specifies that the data for the URL load should be loaded from the
originating source. No existing cache data should be used to satisfy a
URL load request.
https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy
Use removeCachedResponseForRequest: method of NSURLCache class.
Documentation link
I have three UIwebView added from story board when i load them i add activity indicator to show loading progress and when loading is finish. Delegate of DidFinishLoading called. But i see the white color on uiwebview. When i again load the UIwebview when white screen is visible then it shows the error "Operation couldn't be completed" but when loading of UIwebview is properly done and again i reload with new url. then it does not show any error.
Please somebody tell me.. Thanks in advance.
NSString* urlgmap=#"www.google.com"
NSURL *url_gmap = [NSURL URLWithString:urlgmap];
NSURLRequest *request_gmap = [NSURLRequest requestWithURL:url_gmap cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[self.wrae_one loadRequest:request_gmap];
NSString* urlgrid=#"www.yahoo.com";
NSURL *url_grid = [NSURL URLWithString:urlgrid];
NSURLRequest *request_grid = [NSURLRequest requestWithURL:url_grid cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[self.wrae_two loadRequest:request_grid];
NSString* urlSmap=#"www.facebook.com";
NSURL *url_smap = [NSURL URLWithString:urlSmap];
NSURLRequest *request_smap = [NSURLRequest requestWithURL:url_smap cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
How would you detect any attempt to display an SSL URL that contains non-SSL assets in WebView? Just like a browser gives you a mixed-SSL warning.
I don't see any obvious property on UIWebView or UIWebViewDelegate. I could subclass NSURLProtocol, and somehow communicate the non-SSL connections back to the UIWebView. Is there an easier way to do this?
One solution is to create your custom URL cache and make it the default so you can catch all HTTP requests, and then you can check for mixed SSL content.
Here is an example:
#interface MonitoringURLCache : NSURLCache
#end
#implementation MonitoringURLCache
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest *)request {
NSURL *requestURL = [request URL];
NSURL *pageURL = [request mainDocumentURL];
if ([[pageURL scheme] isEqualToString:#"https"] && [[requestURL scheme] isEqualToString:#"http"]) {
NSLog(#"Non safe resource: %# referenced from page: %#", requestURL, pageURL);
}
return [super cachedResponseForRequest:request];
}
#end
/// Register your custom cache
MonitoringURLCache *cache = [[MonitoringURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
/// Make a request to a website with mixed SSL content
NSURL *url = [NSURL URLWithString:#"https://some-website-with-mixed-ssl-content/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
today, I encountered a problem with NSURLConnection. I want to download the contents of the URL http://api.wunderground.com/api/fs3a45dsa345/geolookup/q/34.532900,-122.345.json. If I simply paste the URL into Safari, I get the correct response. However, if I do the same thing with NSURLConnection, I get a "not found" response. Here's the code I'm using:
NSURL *requestURL = [[NSURL alloc] initWithString:#"same url as above"];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:requestURL];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self
startImmediately:YES];
What's the problem here?
Make sure you're escaping any special characters in the URL string by sending it a stringByAddingPercentEscapesUsingEncoding: message, for example:
NSString *s = [#"some url string" stringByAddingPercentEscapesUsingEncoding:NSUTFStringEncoding];
NSURL *requestURL = [NSURL URLWithString:s];
EDIT
It turns out the web service request is failing because the User-Agent header doesn't get set by default. To set it, use an instance of NSMutableURLRequest rather than NSURLRequest to create the request, as shown below:
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myURL];
[myRequest setValue:#"My App" forHTTPHeaderField:#"User-Agent"];
Where is the delegate code? For async NSURLConnection there needs to be a delegate method to receive the returned data. Other options include sendSynchronousRequest: or if it must be async wrapping sendSynchronousRequest in a GCD block.