0

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)
    }
}
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Stef
  • 973
  • 1
  • 8
  • 12

2 Answers2

1

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().

  • Thanks for your reply Eldar! This should be the right solution for me! I tried to implement the WKWebsiteDataStore.default() method. `let configuration = WKWebViewConfiguration() configuration.websiteDataStore = WKWebsiteDataStore.default() webView = WKWebView(frame: CGRect.zero, configuration: configuration)` But how do I access this configuration from another class, so it will use these cookies in the other WKWebView and stay logged in? – Stef Sep 27 '17 at 15:45
  • Is this possible using a segue? – Stef Sep 27 '17 at 16:11
  • @Stef You can use property `configuration` to get the initial configuration of webView. WKWebsiteDataStore.default() returns a singleton object. You just need to use this singleton for all newly created web views. – Eldar Pikunov Sep 28 '17 at 11:07
0

The apple site say:

If your app creates multiple web views, assign the same WKProcessPool object to web views that may safely share a process space. Instantiate an instance of this class and assign it to the processPool property of each web view’s WKWebViewConfiguration object.

However, you set your processProtocol into class ViewControler. Then, this is redefined each time the view is instantiate. Do it:

import UIKit
import WebKit
let processPool = WKProcessPool()

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

override func viewDidLoad() {
    super.viewDidLoad()

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

    webView.navigationDelegate = self
    view.addSubview(webView)
}
}
DuDa
  • 3,109
  • 4
  • 13
  • 30