Ste*_*tef 0 webkit swift wkwebview wkwebviewconfiguration
您好,我目前正在我的 Swift 3 项目中使用 WKWebView。是否可以在另一个类中保留相同的 WKWebView,以便您在不同的类中运行一个主 WKWebView?
我尝试使用 WKProcessPool 和 WKWebsiteDataStorage 执行此操作,但是当我在另一个类中设置新的 WKWebView 时,我不知道如何引用 wkwebview 中的现有配置/记录。
我查遍了互联网但找不到解决方案。我希望您理解我的问题并知道解决方案。提前致谢!!
斯特夫。如果您想对所有 Web 视图实例使用相同的配置,只需使用静态属性/方法创建类或扩展即可。例如:
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!
}
Run Code Online (Sandbox Code Playgroud)
如果您只想在 Web 视图实例之间共享 Cookie,请WKWebsiteDataStore.default()为您的WKWebViewConfiguration实例使用 singleton 对象。