nilkash nilkash - 5 months ago 406
iOS Question

Set cookies with NSURLSession

Hi I am developing one Iphone application In which I want to set cookies after server response and use that for another request. My network request looks like.

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
NSLog(@"sttaus code %i", httpResp.statusCode);
if(error)
{
[self.delegate signinWithError:error];
}
else
{
[self.delegate signinWithJson:data];
}
}] resume];


But I don't know how to set cookies. I know I have to use
NSHTTPCookieStorage
but I don't know how to set. And I also want to use that cookies for another request. Is there any one who knows about this? Need help. Thank you.

See I tried in this way

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error)
{


if(error)
{
[self.delegate signinWithError:error];
}
else
{
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie *cookie;

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];


NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];

for(NSString *key in [headers allKeys]) {
NSLog(@"%@ ..PPPP ... %@",key ,[headers objectForKey:key]);
[cookieProperties setObject:[headers objectForKey:key] forKey:key];
}

[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

[self.delegate signinWithJson:data];
}
}] resume];


I am interested in only one header field
Set-Cookie SSID=kgu62c0fops35n6qbf12anqlo7; path=/

Answer

You can probably get away with just using the sharedHTTPCookieStorage for NSHTTPCookieStorage, and then use setCookies:forURL:mainDocumentURL: or the single setCookie: - the latter might be better for your needs.

If this doesn't work you might need to setup the NSURLSessionConfiguration and set the NSHTTPCookieStorage

The docs don't state it, but the defaultSessionConfiguration might use the shared store anyway.

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:url completionHandler:
 ^(NSData *data, NSURLResponse *response, NSError *error)
 {
  NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;

  NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[response URL]];
  [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:[response URL] mainDocumentURL:nil];

  NSLog(@"sttaus code %i", httpResp.statusCode);
  if(error)
 {
    [self.delegate signinWithError:error];
 }
 else
 {
    [self.delegate signinWithJson:data];
 }
}] resume];
Comments