4

I am setting (or attempting to set) an NSHTTPCookie as follows:

+ (void)setCookie {

  NSString* cookieName = @"MyCookieName";
  NSString* cookieValue = @"MyCookieValue";
  NSString* cookieOriginURL = @"www.mycompany.com";
  NSString* cookiePath = @"/";

  NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
  [cookieProperties setObject:cookieName      forKey:NSHTTPCookieName];
  [cookieProperties setObject:cookieValue     forKey:NSHTTPCookieValue];
  [cookieProperties setObject:cookieOriginURL forKey:NSHTTPCookieOriginURL];
  [cookieProperties setObject:cookiePath      forKey:NSHTTPCookiePath];

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

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

}

After this method is called, I create an NSURLRequest:

NSString *urlAddress = @"http//:www.mycompany.com/mobile/home";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

NSLog(@"Here's the request: %@", [requestObj description]);

//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];

But the output is:

Here's the request: <NSURLRequest: 0xa33a4d0> { URL: http:www.mycompany.com/mobile/home

I expected to see the cookie info appended to the request, but it is not.

I don't know much about cookies, so I don't know if my code is missing something, or if I'm just miss interpreting what the output means.

Thanks for any help.

Hap
  • 556
  • 1
  • 6
  • 20

1 Answers1

2

I doubt that the description of the NSURLRequest will provide the cookie information.

The domains are a match (cookie and the URL), so the cookie must be appended to the request. Execute the following code before firing the request to see which cookies are sent along with your request.

NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie *cookie in [cookieJar cookiesForURL:url]) {
        NSLog(@"Cookies attached: %@", cookie.description);
}
Guven
  • 2,280
  • 2
  • 20
  • 34
  • You were correct that the NSURLRequest description didn't have the info I was expecting. This led me to my actual problem. My NSHTTPCookieOriginURL and actual URL didn't match. I just had to add "http://" to the NSHTTPCookieOriginURL. That did the trick. And your suggestion helped me to verify that. Thanks. – Hap Dec 05 '13 at 22:07
  • 1
    Happy to hear that the issue is resolved. I didn't know that the `"http://"` was required for the cookie to match the URL; I thought only the domain had to be a match. I will research that a bit! – Guven Dec 06 '13 at 09:10
  • 1
    Also, I am pretty sure you have discovered that yourself, you can iterate over all the cookies instead of the ones that match the url. For that, just change `[cookieJar cookiesForURL:url]` to `[cookieJar cookies]`. – Guven Dec 06 '13 at 09:12
  • 1
    Your comments led me to investigate NSHTTPCookieOriginURL vs NSHTTPCookieDomain. I wasn't setting the domain, only the origin url. The documentation says "A value must be specified for either NSHTTPCookieDomain or NSHTTPCookieOriginURL." But apparently, when you use only NSHTTPCookieOriginURL, you must explicitly add "http://". Otherwise, if I set NSHTTPCookieDomain to @"www.mycompany.com" (notice, no "http://") then the NSHTTPCookieOriginURL doesn't have to have the "http://" and the cookie is still set as expected. – Hap Dec 06 '13 at 14:03
  • That is great information, thanks for sharing. I had no idea that was the case. – Guven Dec 07 '13 at 17:33