2017-09-15 261 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視圖使用這個單例。 –