Can't clear UIWebView cache - ios

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

Related

Local HTML Cache policy in UIWebView

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

Clearing Caches from safari in iOS

i have an app that loads pdf file from server in UIWebView
when i change the pdf file from the server it does't changes in the app, i tried all this methods
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:MyURLString]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
still the new pdf file doesn't change , sometimes it take some time to change and sometimes it don't
is there any other methods ?
it's weird that neither [[NSURLCache sharedURLCache] removeAllCachedResponses]; nor [[NSURLCache sharedURLCache] removeAllCachedResponses]; works for you.
you may try one more (although not nice) trick:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(request.cachePolicy != NSURLRequestReloadIgnoringLocalCacheData) {
NSURLRequst* noCacheRequest = [[NSURLRequest alloc] initWithURL:request.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:request.timeoutInterval];
[webView loadRequest:noCacheRequest];
return false;
}
else
return true;
}
also, try setHTTPShouldHandleCookies:, like here

How to get cookie from Webserver and maintain it in iOS app?

I would like to to get fetch JSON from http://mycompany.com/page1...http://mycompany.com/page2... On the the webserver side, it requires initial login http://mycompany.com/login, and after that a cookie is maintained for the user. How do I get this behavior with NSURLConnection without having to ask for login every time? Here is the non-working code using NSURLCredential Storage. Do I need to get the cookie from webservice at loging and then send it along with later requests? I struggled with this for some time, So can you please clarify your answer.
- (IBAction)getJSON:(id)sender
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:#"user"
password:#"pass"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:#"myCompany.com"
port:0
protocol:#"http"
realm:nil
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
//////////GET JSON//////////////
NSError *error;
NSURL *url = [NSURL URLWithString:#"http://mycompany.com.jsonpage1"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
//I am NOT getting JSON in this delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#",responseString);
}
Reading cookies:
refer to Managing HTTP Cookies on iPhone
Setting cookie:
... set dictionary with cookie properties, then:
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjects:object forKeys:keys]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
but keep in mind that session cookies can expire on your server

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

Leaks in NSURLConnection

I have googled a lot and read a lot about the leaks issue with NSURLConnection, but none of them gives a definite answer as to what is the solution to overcome these leaks.
I create an asynchronous connection and every time the connection is established, I get a GeneralBlock-3584 leak. Sometimes the responsible library is shown to be Foundation [NSThread start] frame. Some instances of GeneralBlock-3584 have CFNetwork HostLookup_Master::HostLookup_Master(__CFString const*, InheritEnum<_ExtendedHostInfoType, CFHostInfoType>, __CFHost*, CFStreamError*) as responsible frame.
I tried setting NSURLCache size to 0 as suggested by some. Howvever, even that doesn't work.
Here is what my connector class looks like:
-(void) connectToUrl:(NSString*)urlStr withDelegate:(id)theDelegate{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/xml" forHTTPHeaderField:#"Content-Type"];
self.delegate = theDelegate;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[NSURLConnection connectionWithRequest:request delegate:self];
[pool release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if(xmlResponse == nil){
xmlResponse = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
else{
NSMutableString *temp = [[NSMutableString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
[xmlResponse appendString:temp];
[temp release];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[self.delegate connectionDidFinish:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self.delegate connectionDidFail:self];
}
And I am calling this connector class's connectToUrl:(NSString*)urlStr withDelegate:(id)theDelegate method as below:
Connector *con = [[Connector alloc] init];
[con connectToUrl:urlStr withDelegate:self];
I am releasing 'con' in connectionDidFinish and connectionDidFail methods of delegate class.
Please suggest a solution for the GeneralBlock-3584 leaks. I have been racking my brains for long.
The leaks have gone away since iOS4.

Resources