2017-06-21 61 views
-1

我想刪除我的應用程序的緩存。 我想通過使用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{ 

到視圖控制器,因此 「預期聲明」 的錯誤發生。 我的代碼有什麼問題? 我應該在某些時候連接我的Storyboard和這段代碼嗎? 我該如何解決這個問題?

+3

行'web瀏覽器:(UIWebView的*)瀏覽器上shouldStartLoadWithRequest:(*的NSURLRequest)請求navigationType:(UIWebViewNavigationType)navigationType'你補充說,這是一個Objective- C代碼。在swift中轉換並再次運行代碼。 –

+0

https://stackoverflow.com/a/40145732/6656894參考這個答案@ user8080149 –

回答

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) 

它的目標C code.Update你的代碼像下面

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. 
    } 


} 
+0

thx,你的意見。我有一個關於烏爾代碼的問題。 FUNC web視圖(_ web視圖:UIWebView中, shouldStartLoadWith要求:的URLRequest, navigationType:UIWebViewNavigationType) - >布爾 //做你的東西 }的我們的代碼 是刪除緩存的一部分,對不對? – user8080149

+0

@ user8080149檢查我的更新答案 –

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) 
     } 
    } 
}