Local HTML Cache policy in UIWebView - ios

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];

Related

how do I create a persistent Cache using UiWebView with objective-c?

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.

Objective-c clear cache NSCachedURLResponse

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

How to clear web cache in iOS

I tried the following way:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
It clears the cache.db but it is still in memory.
It does not delete it permanently.
I am able to read it through "strings"
Does anybody know how to purge cache.db in iOS(pragmatically)
Try this,
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

Can't clear UIWebView cache

I can't seem to find a way to clear UIWebView cache. I have tried the following, but nothin worked so far:
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:_request];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
_request = nil;
[NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
_request = [NSMutableURLRequest requestWithURL:t_url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
// also tried NSURLRequestReloadIgnoringLocalAndRemoteCacheData and NSURLRequestReloadIgnoringCacheData
timeoutInterval:10.0];
[_request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[_request setHTTPShouldHandleCookies:NO];
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil; // Actually never gets called
}
Anybody came across this? Thanks!
You seem to have tried almost everything. I'm aware you are rejecting the cookies. Yet, Why dont you try this? This worked for me...
NSHTTPCookie *aCookie;
for (aCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:aCookie];
}

NSXMLParser Cache

I'm developing an application that uses the xml parsing. Everything works wing perfection but when I edit the xml file changes are not displayed. I think it may be a matter of cache because the file is loaded correctly on the server. how do I fix it?
-(IBAction)avviaParsing{
NSString *urlstring=#"http://www.acicastelloonline.it/app_ios/show.xml";
NSURL *xmlURL=[NSURL URLWithString:urlstring];
NSXMLParser *parser = [[ NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[parser setDelegate:self];
[parser parse];
[parser release];}
here is three option, you should try this code before XMLParsing:
you select only one of the three.
clearing
[[NSURLCache sharedURLCache] removeAllCachedResponses];
disable cache
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:cache];
[sharedCache release];
this is other solution.
NSURL *xmlURL=[NSURL URLWithString:URL];
NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0.0f];
NSXMLParser *xmlParser =[[NSXMLParser alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

Resources