Shared cookies with WKProcessPool for WKWebView in Swift

ぃ、小莉子 提交于 2021-01-01 17:52:55

问题


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)
    }
}

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




回答2:


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)
}
}


来源:https://stackoverflow.com/questions/46247464/shared-cookies-with-wkprocesspool-for-wkwebview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!