我想删除我的应用的缓存。 我想使用UIWebView显示一个网页。
我目前的代码:
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,所以"预期声明"错误发生。 我的代码有什么问题? 我应该在某些时候连接我的故事板和这段代码吗? 我该如何解决这个问题?
答案 0 :(得分:0)
要删除缓存,请尝试以下代码
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)
它是Objective 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.
}
}
答案 1 :(得分:0)
您使用的是Objective-C
代码shouldStartLoadWithRequest:
以下是加载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)
}
}
}