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 theloadView()
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
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
Sharing an Image Between Two Viewcontrollers During a Transition Animation
Undefined Symbols for Architecture I386: "_Objc_Class_$_Zipexception", Referenced From: Error
Are View Controllers with Nib Files Broken in iOS 8 Beta 5
Correct Way to Setting a Tag to All Cells in Tableview
Convert an Nsurl to an Nsstring
iOS Uialertview Button to Go to Setting App
Uicollectionview Insert Cells Above Maintaining Position (Like Messages.App)
How to Change the Height of a Uinavigationbar in Storyboard Without Using a Uinavigationcontroller
How to Listen for All Notifications Sent to the iOS Nsnotificationcenter's Defaultcenter
Xcode - Symbol(S) Not Found for Architecture X86_64 (iOS Lib)
How to Add a Button with Click Event on Uitableviewcell in Swift
Change Push Notification Sound
Using Cabasicanimation to Rotate a Uiimageview More Than Once
Opengl Es 2.0 to Video on iPad/Iphone