swift - 在 Swift 中清除 UIWebView 的缓存

标签 swift uiwebview

我的应用程序中有一个 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)

有关缓存策略的更多信息:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

关于swift - 在 Swift 中清除 UIWebView 的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28107806/

相关文章:

Swift - 以编程方式自定义 UISearchBar 设计

ios - 如何快速将文本字段的文本带到渐变背景的前面

javascript - 检测 UIWebView 上的触摸但忽略交互元素上的滑动或触摸(链接或点击)

javascript - iPhone SDK : Submit a UIWebview programmatically

iphone - 如何以编程方式缩放网页 View ?

html - Swift 3 - 撇号在屏幕上被转换为 â

ios - 在 ARSession 期间打开手电筒

ios - 如何知道主题创建是否为 "submitted"到控制台(Firebase 云消息传递)

ios - Objective-C 发送按键事件到 webview

swift 结合。如何转变出版商值(value)观