2017-09-15 255 views
0

任何人都可以请告诉我如何在Swift中创建WKProcessPool?我不熟悉Objective-C。 为了与所有WKWebViews共享cookie,我必须创建一个WKProcessPool。即使在显示具有相同类别的另一个视图控制器时,我也想保留cookie。我尝试了以下,但它不工作。在Swift中使用WKProcessPool与WKWebView共享cookie

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

您需要使用configuration.websiteDataStore属性而不是processpool。

对于存储的cookie,使用WKWebsiteDataStore.default()值。 对于私人浏览使用WKWebsiteDataStore.nonPersistent()。

+0

感谢您的回复Eldar!这对我来说应该是正确的解决方案!我试图实现WKWebsiteDataStore.default()方法。 '让配置= WKWebViewConfiguration() configuration.websiteDataStore = WKWebsiteDataStore.default() webView = WKWebView(frame:CGRect.zero,configuration:configuration)'但是如何从另一个类访问这个配置,所以它会使用这些cookie在其他WKWebView并保持登录状态? – Stef

+0

这可能使用segue吗? – Stef

+0

@Stef您可以使用属性'configuration'来获取webView的初始配置。 WKWebsiteDataStore.default()返回一个单例对象。你只需要为所有新创建的Web视图使用这个单例。 –