Luoruize Luoruize - 1 year ago 179
Swift Question

Disabling cookies in WKWebView

Is it possible at all to disable cookies and local storage in a WKWebView?

Let's say that this is my setup, and I want to add something that disables them:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}

override func viewDidLoad() {
super.viewDidLoad()

let myURL = URL(string: "http://bla.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}


}

Answer Source

To disable cookies:

override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = URL(string: "http://bla.com")
    var myRequest = URLRequest(url: myURL!)
    myRequest.httpShouldHandleCookies = false
    webView.load(myRequest)
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download