说起WKWebView,性能好,性能高,闻风丧胆的操作应该是怎么设置cookie,查了很多方法,设置cookie有很多种方法,还要兼容老版本,通过setCookie{}block设置,有的时候操作失败,是什么原因都不知道。通过多次尝试,通过js添加,发现成功率100%,方法如下:
let myCookie = "document.cookie='usercenter.id=\(Account.default);Domain=\(APIService.server); Path=/;'"
let script = WKUserScript(source: myCookie, injectionTime: .atDocumentStart, forMainFrameOnly: true)
web = WKWebView()
web.navigationDelegate = self
web.configuration.userContentController.addUserScript(script)
注意,cookie=‘’(单引号),如果弄错,设置还是失败的,建议赶紧替换UIWebView吧,有内存泄漏,而且性能低,加载一个h5界面,需要半个小时