title: WKWebView Cookies
body: <p>I'm using the below mentioned approach to set cookies in a <code>WKWebview</code>:
  <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>
  
  <p>But the cookies that I have set are being duplicated in the AJAX calls. I mean they are being repeated twice.</p>
  
  <p>Here is the code snippet that I used:</p>
  
  <pre><code>NSString *strURL = DASHBOARDURL;    
  NSURL *url = [NSURL URLWithString:strURL];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setURL:url];
  
  NSMutableString *script = [[NSMutableString alloc] init];
  NSMutableString *cookieString = [[NSMutableString alloc] init];
  
  for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
      [script appendString:[NSString stringWithFormat:@"document.cookie='%@';",cookie.getCookieString]];
      [cookieString appendString:[NSString stringWithFormat:@"%@;", cookie.getCookieString]];
  }
  [request setValue:cookieString forHTTPHeaderField:@"Cookie"];
  
  //cookies for further AJAX calls
  WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  WKUserScript *cookieInScript = [[WKUserScript alloc] initWithSource:script
                                                        injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                     forMainFrameOnly:YES];
  [userContentController addUserScript:cookieInScript];
  
  WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init];
  webViewConfig.userContentController = userContentController;
  
  CGRect viewRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
  
  wkWebview = [[WKWebView alloc] initWithFrame:viewRect configuration:webViewConfig];
  wkWebview.navigationDelegate = self;
  [wkWebview loadRequest:request];
  [self.view addSubview:wkWebview];
  </code></pre>
  
  <p><code>getCookieString</code> is a method that returns cookie values as an <code>NSString</code></p>
  
  <ol>
  <li>Will the <code>WKWebView</code> set the cookies back to <code>NSHTTPCookieStorage</code>
  at runtime(During AJAX calls)</li>
  <li>Can i control AJAX calls cookies with any delegate methods?</li>
  </ol>
  
  <p>The following is my <code>getCookieString</code> category(<code>NSHTTPCookie (CookieObject)</code>) method</p>
  
  <pre><code>- (NSString *)getCookieString {
  
    NSString *string = [NSString stringWithFormat:@"%@=%@;domain=%@;expiresDate=%@;path=%@;sessionOnly=%@;isSecure=%@",
                      self.name,
                      self.value,
                      self.domain,
                      self.expiresDate,
                      self.path ?: @"/",
                      self.isSecure ? @"TRUE":@"FALSE",
                      self.sessionOnly ? @"TRUE":@"FALSE"];
  
    return string;
  }
  </code></pre>