我的应用程序中有一个 UIWebView。每次 viewDidLoad
时,都需要完全重新加载此 UIWebView(即清除所有图像/HTML/cookie 等缓存)。
那么我可以在 Swift 中执行此操作吗?
这是我的代码:
let myUrl = NSURL(string: "http://www.example.com")
let myRequest = NSURLRequest(URL: myUrl!)
myWebView.loadRequest(myRequest)
谢谢!
最佳答案
你可以使用
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
您还可以更改 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)
关于swift - 在 Swift 中清除 UIWebView 的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28107806/