Reputation: 1273
Hi I'm currently using WKWebView in my Swift 3 project. Is it possible to keep the same WKWebView in another class, so that you are running one main WKWebView in different classes?
I've tried to do this with WKProcessPool and WKWebsiteDataStorage, but when I setup a new WKWebView in the other class I don't know how to reference to the existing configuration/logged in wkwebview.
I've looked all over the internet but couldn't find a solution. I hope you understand my problem and know a solution. Thanks in advance!!
Upvotes: 0
Views: 1486
Reputation: 96
Stef. If you want to use the same configuration for all web view instances, just create class or extension with a static property/method. For example:
extension WKWebViewConfiguration {
static var shared : WKWebViewConfiguration {
if _sharedConfiguration == nil {
_sharedConfiguration = WKWebViewConfiguration()
_sharedConfiguration.websiteDataStore = WKWebsiteDataStore.default()
_sharedConfiguration.userContentController = WKUserContentController()
_sharedConfiguration.preferences.javaScriptEnabled = true
_sharedConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = false
}
return _sharedConfiguration
}
private static var _sharedConfiguration : WKWebViewConfiguration!
}
If you want only share cookies between web view instances, use singleton object by WKWebsiteDataStore.default()
for your WKWebViewConfiguration
instances.
Upvotes: 6