Cookie Sharing Between Multiple Wkwebviews

Cookie sharing between multiple WKWebViews

Got this working by using the same WKProcessPool for all the webviews.

First create a process pool once somewhere:

processPool = [[WKProcessPool alloc] init];

Then use it when creating WKWebviews. The pool must be set in the init method, not afterwards.

WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.processPool = processPool;
webview = [[WKWebView alloc] initWithFrame:frame configuration:config];

Cookies Sharing between wkwebviews

Try the following code in your loadView() for both of your ViewController classes:

webConfiguration.processPool = viewHome.processPool

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = viewHome.processPool
webViewHome = WKWebView(frame: .zero, configuration: webConfiguration)

Additional:For standard, please Use the camel case in the Class name convention (viewHome -> ViewHome).

WKWebViews (multiple) - sharing cookies, localStorage in Swift

I think it is best if you use SFSafariViewController for your needs.

As the documentation states:

In iOS 9 and 10, it shares cookies and other website data with Safari.

It means, that it is going to use the same cookies and data from the Safari browser, which is even better. If I am not mistaken, the user can be logged in through Safari, and when he comes to your app, he will not have to log in again.

Here is the full documentation for it:

SFSafariViewController

Update:

If you still want to do what you already started, according to this answer here in Objective-C, this is the solution in Swift:

You need a place where you would store the persistent 'process pool'. In your case, it is YourModelObject singleton

class YourModelObject {
static let sharedInstance = YourModelObject()
let processPool = WKProcessPool()
}

Use the shared processPool before initializing the webView. This is the initialization function which you would call in the loadView() for every viewController:

override func loadView() {
super.loadView() //don't forget this line

setupWebView()
}

private func setupWebView() {

let config = WKWebViewConfiguration()
config.processPool = YourModelObject.sharedInstance.processPool
self.webView = WKWebView(frame: .zero, configuration: config)
self.webView.navigationDelegate = self
self.webView.scrollView.delegate = self
self.view = self.webView
}


Related Topics



Leave a reply



Submit