Ios 删除应用程序的缓存
我想删除我的应用程序的缓存。 我想使用UIWebView显示网页 我当前的代码:Ios 删除应用程序的缓存,ios,swift,xcode,Ios,Swift,Xcode,我想删除我的应用程序的缓存。 我想使用UIWebView显示网页 我当前的代码: import UIKit class ViewController: UIViewController { @IBOutlet weak var webBrowser: UIWebView! webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(U
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var webBrowser: UIWebView!
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://127.0.0.1:8000/admin/accounts/")
let request = NSURLRequest(url:url! as URL)
self.webBrowser.loadRequest(request as URLRequest)
// Do any additional setup after loading the view, typically from a nib.
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当我添加此代码时
webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
到ViewController,因此发生“预期声明”错误。
我的代码有什么问题?
我应该在某种程度上连接我的故事板和代码吗?
如何修复此问题?要删除缓存,请尝试下面的代码
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
否则,您还可以更改NSURLRequest的缓存策略
let url = URL(string: "http://127.0.0.1:8000/admin/accounts/")
let request = URLRequest(url: url!,
cachePolicy:NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0)
self.webBrowser.loadRequest(day_url_request)
这是目标C代码。按如下方式更新代码
class ViewController: UIViewController,UIWebViewDelegate {
@IBOutlet weak var webBrowser: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string:"http://127.0.0.1:8000/admin/accounts/")
let request = NSURLRequest(url:url! as URL)
self.webBrowser.delegate = self
self.webBrowser.loadRequest(request as URLRequest)
// Do any additional setup after loading the view, typically from a nib.
}
func webView(_ webView: UIWebView,
shouldStartLoadWith request: URLRequest,
navigationType: UIWebViewNavigationType) -> Bool
// do your stuffs
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您正在使用
Objective-C
codeshouldStartLoadWithRequest:
以下是要加载的swift版本代码UIWebView
class ViewController: UIViewController,UIWebViewDelegate {
override func viewDidLoad() {
var webBrowser: UIWebView!
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
webBrowser = UIWebView(frame: UIScreen.main.bounds)
webBrowser.delegate = self
view.addSubview(webBrowser)
if let url = URL(string: "http://127.0.0.1:8000/admin/accounts/") {
let request = URLRequest(url: url)
webBrowser.loadRequest(request)
}
}
}
您添加的行
webBrowser:(UIWebView*)webBrowser应该使用request:(NSURLRequest*)request-navigationType:(UIWebView-navigationType)navigationType
这是一个Objective-C代码。在swift中转换它并再次运行代码。参考这个答案@user8080149thx,你的评论。我有一个关于你代码的问题。func webView(webView:UIWebView,shouldStartLoadWith request:URLRequest,navigationType:UIWebView navigationType)->Bool//do your stuff}您的代码是删除缓存的一部分,对吗?