title: Swift NSURLSession NSURLRequest Cookies are not accepted
body: <p>I'm new to swift and OS X programming. For my first steps I wanted to create a command line tool which logs me into the webinterface of my mobile carrier, and then shows the amount of my data left. I began to write a wrapper around NSURLSession to make things easier for me.</p>
  
  <p>Problem: My program won't accept cookies.</p>
  
  <p>I tried a lot of things, also setting cookie policies on the session object but nothing changed. How can I make my program accept cookies and how to use them in subsequent requests?</p>
  
  <p><em>HttpClient.swift:</em></p>
  
  <pre><code>import Foundation
  
  class HttpClient {
  
      private var url: NSURL!
      private var session: NSURLSession
  
      internal init(url: String) {
          self.url = NSURL(string: url)
          self.session = NSURLSession.sharedSession()
          session.configuration.HTTPShouldSetCookies = true
          session.configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicy.OnlyFromMainDocumentDomain
          session.configuration.HTTPCookieStorage?.cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.OnlyFromMainDocumentDomain
      }
  
      internal func sendGet() -&gt; String {
          var ready = false
          var content: String!
          var request = NSMutableURLRequest(URL: self.url)
  
          var task = session.dataTaskWithRequest(request) {
              (data, response, error) -&gt; Void in
              content = NSString(data: data, encoding: NSASCIIStringEncoding) as! String
              ready = true
          }
          task.resume()
          while !ready {
              usleep(10)
          }
          if content != nil {
              return content
          } else {
              return ""
          }
      }
  
      internal func sendPost(params: String) -&gt; String {
          var ready = false
          var content: String!
          var request = NSMutableURLRequest(URL: self.url)
  
          request.HTTPMethod = "POST"
          request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
          request.HTTPBody = params.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: false)
          request.HTTPShouldHandleCookies = true
  
          var task = session.dataTaskWithRequest(request) {
              (data, response, error) -&gt; Void in
              content = NSString(data: data, encoding: NSASCIIStringEncoding) as! String
              ready = true
          }
          task.resume()
          while !ready {
              usleep(10)
          }
          if content != nil {
              return content
          } else {
              return ""
          }
      }
  
      internal func setUrl(url: String) {
          self.url = NSURL(string: url)
      }
  }
  </code></pre>
  
  <p><em>main.swift</em></p>
  
  <pre><code>import Foundation
  
  let loginPage = "https://service.winsim.de/"
  let dataPage = "https://service.winsim.de/mytariff/invoice/showGprsDataUsage"
  
  var hc = HttpClient(url: loginPage)
  println(hc.sendPost("usernameField=username&amp;passwordfield=password"))
  hc.setUrl(dataPage)
  println(hc.sendGet())
  </code></pre>