Session is not maintained between UIWebView
<p>In my app I'm trying to login user through UIWebView. On successful login cookies are set in <code>NSHTTPCookieStorage</code>. Few of my app pages are open in UIWebView. </p>

<p>When request for particular web page is send, it check whether user is logged in or not based on the cookies.</p>

<p>I checked that cookies are present in <code>NSHTTPCookieStorage</code>, but are not valid cookies on server. That is, it consider user as logged out user.</p>

<p>My code for loading UIWebView is as below:</p>

<pre><code>let url = serverURL + urlString
let urlRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
webPage.loadRequest(urlRequest)
</code></pre>

<p>Even I tried with <code>NSURLSession</code> and setting cookies as HTTPHeaderField. Below is my code for that too:</p>

<pre><code>let URLRequest: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url)!)

let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(NSURL(string: serverURL)!)
for cookie in cookies!{
    URLRequest.setValue(cookie.value, forHTTPHeaderField: cookie.name)
}

let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)

let task = session.downloadTaskWithRequest(URLRequest)
task.resume()
</code></pre>

<p>It's working properly for few cases. I'm unable to find what can be an issue with the cookies.</p>

<p>Any help will be appreciated.</p>

<p>Thanks in advance</p>