Clearing Caches from safari in iOS - 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

Related

WKWebView session expired

I have a URL that works on a normal web Page, but embedded in a WKWebView I am always getting the error session expired. Is there some way to enable cookies on this WKWebView?
You can save a cookie from response
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSDictionary *headers = [(NSHTTPURLResponse *)navigationResponse.response allHeaderFields];
if ([headers objectForKey:#"Set-Cookie"] != nil) {
_cookie = [headers objectForKey:#"Set-Cookie"];
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
And set the cookie in request
_request = [[NSMutableURLRequest alloc] initWithURL:url];
[_request setValue:_cookie forHTTPHeaderField:#"Cookie"];
[_webView loadRequest:_request];

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

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

UIWebview enable cookies

I am trying to access a specific URL that requires cookies through UIWebView but I can not access it because cookies are disabled. So I did the following:
Enabled cookies:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
Created NSURLConnection and extracted cookies from response:
NSArray *cookies = [ NSHTTPCookie cookiesWithResponseHeaderFields: [ httpResponse allHeaderFields ] forURL:response.URL];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies: cookies forURL: response.URL mainDocumentURL:nil];
But neither of this didn't help. However if I launch the URL in safari it loads successfully and after that I can load the same URL in UIWebView too. Could you help me with this, how can I enable cookies for UIWebView?
Thanks in advance
After create a NSURLRequest, copy all cookies in sharedHTTPCookieStorage to NSURLRequest:
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:YES];
[self addCookies:cookies forRequest:request];
[_webView loadRequest:request];
And add addCookies:forRequest method
- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
if ([cookies count] > 0)
{
NSHTTPCookie *cookie;
NSString *cookieHeader = nil;
for (cookie in cookies)
{
if (!cookieHeader)
{
cookieHeader = [NSString stringWithFormat: #"%#=%#",[cookie name],[cookie value]];
}
else
{
cookieHeader = [NSString stringWithFormat: #"%#; %#=%#",cookieHeader,[cookie name],[cookie value]];
}
}
if (cookieHeader)
{
[request setValue:cookieHeader forHTTPHeaderField:#"Cookie"];
}
}
}

Resources