Shared cookies with WKProcessPool for WKWebView in Swift

Stef picture Stef · Sep 15, 2017 · Viewed 6.9k times · Source

Can anyone please tell me how to create a WKProcessPool in Swift? I'm not familiar with Objective-C. I have to create a WKProcessPool in order to have shared cookies with all WKWebViews. I want to keep cookies even when showing another viewcontroller with same class. I tried the following but it's not working.

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    var webView = WKWebView()

    override func viewDidLoad() {
        super.viewDidLoad()

        let processPool = WKProcessPool()
        let configuration = WKWebViewConfiguration()
        configuration.processPool = WKProcessPool()

        webView.navigationDelegate = self
        view.addSubview(webView)
    }
}

Answer

Eldar Pikunov picture Eldar Pikunov · Sep 25, 2017

You need to use configuration.websiteDataStore property instead of processpool.

For the stored cookies use WKWebsiteDataStore.default() value. For the private browsing use WKWebsiteDataStore.nonPersistent().