1

I have looked at several option to do this but cannot make it work.

There are two places where I would like to clear the cookie and other data that is stored. I have a logout button and I would like to clear the login information there. And then when the user opens up a webkit webpage so that the information is not stored there either. I looked here but this one keep giving me errors where its not formatted right, so the ide keeps saying add a comma or add a ; ect... so this did not work How to delete WKWebview cookies I would like to do is on the view on load of the webkit is to delete the cookies and stuff and then load the webpage. But how can I in swift? Here is the code that I am using

class RegisterViewController: UIViewController, WKNavigationDelegate , WKUIDelegate{

let url = “url”

@IBOutlet weak var containerView: UIView!
var webView: WKWebView?

@IBOutlet weak var progressView: UIProgressView!

@IBOutlet weak var backButton: UIBarButtonItem!
@IBAction func barBackButtonAction(sender: AnyObject) {
    if self.webView!.canGoBack {
        self.webView!.goBack()
    }
}
@IBOutlet weak var forwardButton: UIBarButtonItem!
@IBAction func barForwardButtonAction(sender: AnyObject) {
    if self.webView!.canGoForward{
        self.webView!.goForward()
    }
}

override func viewDidAppear(animated: Bool){
    super.viewDidAppear(animated)
    if(webView != nil)
    {
        let w = self.containerView.frame.width
        let h = self.containerView.frame.height
        webView!.frame = CGRect(x: 0, y: 0, width: w, height: h)
        self.containerView.addSubview(self.webView!
    }

}

override func viewDidLoad() {
    super.viewDidLoad()
    cookieMonster()

   self.webView = WKWebView()
    self.webView?.navigationDelegate = self
    self.webView?.UIDelegate = self
    self.webView!.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)

   let req = NSURLRequest(URL: url!)

    self.webView!.loadRequest(req)
    self.webView!.allowsBackForwardNavigationGestures = true


}

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    print("WebView content loaded.")
    cookieMonster()
    progressView.setProgress(0.0, animated: false)
    checkToSeeIfICanGoBack()
}
func webView(webView: WKWebView,didStartProvisionalNavigation navigation: WKNavigation)
{
   cookieMonster()

}

func cookieMonster()
{
    let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    for cookie in storage.cookies! {
        storage.deleteCookie(cookie)
        print("Eating cookies")
    }
    NSUserDefaults.standardUserDefaults().synchronize()
}


override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>){
    if (keyPath == "estimatedProgress") {
        progressView.hidden = webView!.estimatedProgress == 1
        progressView.setProgress(Float(webView!.estimatedProgress), animated: true)
    }
}

}

Community
  • 1
  • 1
MNM
  • 2,673
  • 6
  • 38
  • 73

0 Answers0