在 Swift 中清除 UIWebView 的缓存 [英] Clearing UIWebView's Cache in Swift

查看:36
本文介绍了在 Swift 中清除 UIWebView 的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 UIWebView.每次 viewDidLoad.

I have a UIWebView inside my app. This UIWebView needs to be reloaded fully (i.e. clear all cache of images/HTML/cookies etc.) every time when viewDidLoad.

那么有什么代码可以在 Swift 中做到这一点吗?

So is there any code I can do this in 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

斯威夫特 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)

斯威夫特 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

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

这篇关于在 Swift 中清除 UIWebView 的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆