如何在 wkwebview/uiwebview 中支持 incognito/private 模式

how to support incognito/private mode in wkwebview/uiwebview

我正在研究一个 incongnito browser.I 当我清除所有 cookie 时我正在使用 wkwebview 我可以看到像 google 这样的流行搜索引擎会记住已经进行的搜索。

我尝试使用 NSURLSession 清理 NSHTTPCookieStorage 和 resetcookies 中的所有 cookie,但它仍然无法正常工作。

设置 nonpersistentdatastorewkwebsitedatastorewkwebviewconfigurationwkwebview

uiwebview

中为 NSURlrequest 设置 NSURLrequestreloadcacheignoringlocalandremotecachedata

参考

使用 WKWebView

在 iOS 中进行隐私浏览

根据苹果文档为了支持隐私浏览,创建一个数据存储对象并将其分配给 WKWebViewConfiguration 对象的 websiteDataStore 属性,然后再创建您的 Web 视图。 default() 方法 returns 将网站数据永久保存到磁盘的默认数据存储。要实现隐私浏览,请改为使用 nonPersistent() 方法创建非持久数据存储。

    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.processPool = WKProcessPool()
    webConfiguration.websiteDataStore = WKWebsiteDataStore.nonPersistent()
    let webView = WKWebView(frame: self.webContainerView.bounds, configuration: webConfiguration)
    // Set up request
    if let requestURL = URL(string: "enter_url_to_load") {
        var request = URLRequest(url: requestURL)
        request.httpShouldHandleCookies = false
        request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        webView.navigationDelegate = self
        webView.load(request)
    }
    self.webContainerView.addSubview(webView)