title: How to inject a cookie into WKWebView or WKWebSiteDataStore w/o JavaScript?
body: <p>I'm trying to set a WKWebView process pool's per-domain cookie value explicitly / programatically. I want to emulate exactly what happens when a server sends back a <code>Set-Cookie</code> header. The research I've done here and elsewhere says to inject that as JavaScript. I don't like that on principle -- it feels wrong. I want to talk to the "guy in charge" (WebKit internal cookie data structure) not inject a hack at the last second through a second language (JavaScript).</p>
  
  <p>I'm <em>also</em> getting a maddening 1% failure rate of this method, likely due to 1) The highly asynchronous nature of <code>WKWebView</code> interaction, since all the networking and JavaScript computation happens in a separate process and 2) The fact that I'm reusing the same <code>WKWebView</code> multiple times when a login session expires, and resetting the <code>document.cookie</code> script just before forcing the page to reload.</p>
  
  <p>I'm surprised there's nothing like <code>WKWebSiteDataStore.addCookie(value: String, inDomain: NSURL)</code>; the fact that it has <code>removeDataOfTypes(...)</code> without <code>add</code> or <code>inject</code> screams "gaping design hole" to me.</p>
  
  <p>References:</p>
  
  <p><a href="https://stackoverflow.com/questions/32839938/ios9-multiple-wkwebview-sharing-cookie-wkprocesspool-doesnt-work">ios9 - Multiple WkWebView sharing cookie WKProcessPool() doesn&#39;t work?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview">Can I set the cookies to be used by a WKWebView?</a></p>