title: Swift: How to remember cookies for further http requests
body: <p>I am working on a login application. After successful login response comes back with cookie data.<br/>
  How can I use/save this data for my future requests?<br/>
  For starters I am trying to save it in <code>NSHTTPCookieStorage</code>. But that is also not working.
  <br/>
  <strong>Login Method(partial):</strong></p>
  
  <pre><code>let task = session.dataTaskWithRequest(request) { (data, responseData, error) -&gt; Void in
              if let response = responseData as? NSHTTPURLResponse {
                  statusCode = response.statusCode
                  print("Response code: \(statusCode)")
              }
  
              var json: NSDictionary?
              do {
                  json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
              } catch {
                  print(error)
                  err = error
              }
  
              if(statusCode != 200) {
  
                  let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                  print("Error could not parse JSON: '\(jsonStr)'")
              }
              else {
  
                  print("Everything Looks good: \(responseData)")
                  self.setCookies(responseData!)
                  self.shouldPerformSegueWithIdentifier("showHomeController", sender: self)
  
              }
          }
  
          task?.resume()
  </code></pre>
  
  <p><strong>Save Cookie Method</strong></p>
  
  <pre><code>private func setCookies(response: NSURLResponse) {
          if let httpResponse = response as? NSHTTPURLResponse {
              let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(httpResponse.allHeaderFields, forURL: response.URL!) as! [NSHTTPCookie]
              NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookies(cookies, forURL: response.URL!, mainDocumentURL: nil)
              for cookie in cookies {
                  var cookieProperties = [String: AnyObject]()
                  cookieProperties[NSHTTPCookieName] = cookie.name
                  cookieProperties[NSHTTPCookieValue] = cookie.value()
                  cookieProperties[NSHTTPCookieDomain] = cookie.domain
                  cookieProperties[NSHTTPCookiePath] = cookie.path
                  cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: cookie.version)
                  cookieProperties[NSHTTPCookieExpires] = NSDate().dateByAddingTimeInterval(31536000)
  
                  let newCookie = NSHTTPCookie(properties: cookieProperties)
                  NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie!)
  
                  println("name: \(cookie.name) value: \(cookie.value())")
              }
          }
      }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Cannot invoke 'cookiesWithResponseHeaderFields' with an argument list of type '([NSObject : AnyObject], forURL: NSURL)'
  </code></pre>