-1

I want to delete the cache of my app. I want to show a web page by using UIWebView.

MY current code:

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


}

When I added this code

webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

to ViewController,so "Expected declaration" error happen. What is wrong in my code? Should I connect my Storyboard and this code in some point? How can i fix this?

user8080149
  • 59
  • 1
  • 7
  • 3
    The line `webBrowser:(UIWebView *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType` you added, it's a Objective-C code. Convert it in the swift and run the code again. – bestiosdeveloper Jun 21 '17 at 10:04
  • https://stackoverflow.com/a/40145732/6656894 refer this answer @user8080149 – Himanshu Moradiya Jun 21 '17 at 10:08

2 Answers2

2

To delete cache try below code

URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0

Else you can also change the cache policy of the 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)

it's Objective C code.Update your code like below

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


}
Dharma
  • 3,007
  • 3
  • 23
  • 38
  • thx,ur comments.I have a question about ur code. func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool // do your stuffs } of ur code is the part of deleting cache,right? – user8080149 Jun 22 '17 at 00:55
0

You are using Objective-C code shouldStartLoadWithRequest:

Here is the swift version code to load 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)
        }
    }
}
Bhavesh Dhaduk
  • 1,888
  • 15
  • 30