Clearing UIWebview cache
I actually think it may retain cached information when you close out the UIWebView
. I've tried removing a UIWebView
from my UIViewController
, releasing it, then creating a new one. The new one remembered exactly where I was at when I went back to an address without having to reload everything (it remembered my previous UIWebView
was logged in).
So a couple of suggestions:
[[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];
After that, you can try deleting any associated cookies with the UIWebView
:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
Swift 3:
// Remove all cache
URLCache.shared.removeAllCachedResponses()
// Delete any associated cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
Clearing UIWebView's Cache in Swift
You can use
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0
NSURLCache.sharedURLCache().memoryCapacity = 0
Swift 3.0
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
You can also change the cache policy of the NSURLRequest
let day_url = NSURL(string: "http://www.domain.com")
let day_url_request = NSURLRequest(URL: day_url,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
let day_webView = UIWebView()
day_webView.loadRequest(day_url_request)
Swift 3.0
let day_url = URL(string: "http://www.domain.com")
let day_url_request = URLRequest(url: day_url!,
cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
let day_webView = UIWebView()
day_webView.loadRequest(day_url_request)
For more information on cache policies : https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/
index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy
How to clear cache on UIWebView in swift?
You can easily do it with ;
NSURLCache.sharedURLCache().removeAllCachedResponses()
NSURLCache.sharedURLCache().diskCapacity = 0
NSURLCache.sharedURLCache().memoryCapacity = 0
Also use like this;
let url = NSURL(string: "http://www.web.com")
let url_request = NSURLRequest(URL: url,
cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 5.0)
let webView = UIWebView()
webView.loadRequest(url_request)
Thanks
Clear UIWebView cache when use local image file
You can try this, in your AppDelegate.m
+(void)initialize {
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
Related Topics
How to Mimic the Bottom Sheet from the Maps App
Uibutton Custom Font Vertical Alignment
Uibutton Inside a View That Has a Uitapgesturerecognizer
How to Use Uivisualeffectview to Blur Image
What Are the Dangers of Method Swizzling in Objective-C
How to Create .Ipa File Using Xcode
How to Increase the Height of Navigation Bar in Xcode
How to Present Popover Properly in iOS 8
Why Uitableviewautomaticdimension Not Working
Xcode 7 Error: "Missing iOS Distribution Signing Identity For ..."
How to See Saved Nsuserdefaults
Xcode 4: Create Ipa File Instead of .Xcarchive
How to Validate an E-Mail Address in Swift
How to Perform Wireless Debugging in Xcode 9 With iOS 11, Apple Tv 4K, etc
Uiview and Initwithframe and a Nib File. How to Get the Nib File Loaded
How to Get the Status of Bluetooth (On/Off) in iPhone Programmatically