2017-06-21 78 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) 
     } 
    } 
}