删除应用的缓存

时间:2017-06-21 10:00:55

标签: ios swift xcode

我想删除我的应用的缓存。 我想使用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,所以"预期声明"错误发生。 我的代码有什么问题? 我应该在某些时候连接我的故事板和这段代码吗? 我该如何解决这个问题?

2 个答案:

答案 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

的swift版本代码
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)
        }
    }
}