项目中Hybird容器升级到WKWebView各种坑

WKWebView 中localstorage不及时同步。processPool单例可以解决

class LocalstorageManger {
    static var sharePreferences = WKPreferences()
    static var shareProcessPool = WKProcessPool()
}
 let configuration = WKWebViewConfiguration()
 configuration.preferences = LocalstorageManger.sharePreferences
 configuration.processPool = LocalstorageManger.shareProcessPool

WKWebView 不支持 NSURLProtocol 坑

就不上代码了,具体请看和github的Demo如下

  • 方案
  • github的Demo

WKWebView 注册cookie,包含域名domain,时间expires等

let configuration = WKWebViewConfiguration()
let userContentController = WKUserContentController()
let cookieValue = String(format:"document.cookie ='platform=%@;path=/;domain=medlinker.com;expires=Sat, 02 May 2019 23:38:25 GMT;';document.cookie = 'sess=%@;path=/;domain=medlinker.com;expires=Sat, 02 May 2018 23:38:25 GMT;';",user.platform,user.sess)
let cookieScript = WKUserScript(source: cookieValue, injectionTime: .atDocumentStart , forMainFrameOnly: false) userContentController.addUserScript(cookieScript) 
configuration.userContentController = userContentController

你可能感兴趣的:(项目中Hybird容器升级到WKWebView各种坑)