Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone nsurlconnection read cookies

I am using async NSURLConnection to connect to a web site from iPhone. Handle didReceiveResponse is activated on response and I am trying to get all cookies, by using allHeaderFields from NSHTTPURLResponse

I see many hreader, but no Set-Cookie - it looks like iphone simulator just ignores them... And I am sure cookies are present in response - network monitor shows they present

I do not use any http storage - all that I am trying to do is to print to log all header - and do not see cookies info

Does anybody know about this issue?

UPDATE I have made some research: if my website returns custom header, like "Custom-Header: value" - then this header is visible in java client, but is not in iphone...

thanks

like image 623
user349302 Avatar asked Jul 23 '10 15:07

user349302


2 Answers

Try to look for it in the shared HTTP cookies storage:

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
    NSLog(@"name: '%@'\n",   [cookie name]);
    NSLog(@"value: '%@'\n",  [cookie value]);
    NSLog(@"domain: '%@'\n", [cookie domain]);
    NSLog(@"path: '%@'\n",   [cookie path]);
}

or if working in Swift:

for cookie in HTTPCookieStorage.shared.cookies!
{
   NSLog("name: \(cookie.name)")
   NSLog("value: \(cookie.value)")
   NSLog("domain: \(cookie.name)")
   NSLog("path: \(cookie.path)")
}
like image 166
Tal Bereznitskey Avatar answered Nov 15 '22 17:11

Tal Bereznitskey


Try this: in your NSMutableURLRequest, you should tell it to handle cookies:

[request setHTTPShouldHandleCookies:YES];
like image 26
Don Avatar answered Nov 15 '22 18:11

Don