IOS在webview加载内容时js注入cookie

在webview加载内容时js注入cookie:

//swift代码
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    let authtoken = 'token'
    let cookie = "authtoken=\(authtoken)"
    // 注入多个key, 使用 ; 连接
    // let cookie = "authtoken=\(authtoken);key=\(value)"
    webView.evaluateJavaScript("function setCookie(e,o){document.cookie=e+\"=\"+escape(o)+\";path=/;domain=abc.com\"}for(var cookieTem= \"\(cookie)\",cookieArr=cookieTem.split(\";\"),i=0;i<cookieArr.length;i++){var temArr=cookieArr[i].split(\"=\");setCookie(temArr[0],temArr[1])}", completionHandler: {
        (object, error) -> Void in
    })
}

引用地址