13

I'm developing a newsstand application and use NSURLRequest to download issue assets.

NSArray *contents = [issue.tableOfContents objectForKey:kSNTableOfContentsContents];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSLog(@"HERE GO MY COOKIES");
for (cookie in [cookieJar cookies]) {
    NSLog(@"%@", cookie);
}            
for (NSDictionary *contentItem in contents) {
    NSString *contentURL_string = [contentItem objectForKey:kSNTableOfContentsRemoteURL];
    NSURL *contentURL = [NSURL URLWithString:contentURL_string];
    NSString *fileName = [contentItem objectForKey:kSNTableOfContentsContentsURL];      
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL];
    NKAssetDownload *asset = [newsstandIssue addAssetWithRequest:request];
    [request release];
    ....
    [asset downloadWithDelegate:self];
    ....
}

When the first for loop is executed my cookies appear to be in NSHTTPCookieStorage, but when actual requests are sent, there are no cookie information in headers. I use CharlesProxy to look that up. Could anyone please give some advice what might be causing this issue?

Hiren
  • 12,720
  • 7
  • 52
  • 72
Alex Petrov
  • 275
  • 2
  • 13
  • You could try doing it manually: http://stackoverflow.com/questions/5954382/ios-is-it-possible-to-set-a-cookie-manually-using-sharedhttpcookiestorage-for-a – Adam Shiemke Mar 30 '12 at 17:59

2 Answers2

18

From this thread, the magic incantation appears to be:

NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:
  [cookieJar cookies]];
[request setAllHTTPHeaderFields:headers];

(Warning: untested code.)

This will convert your cookie jar into an array of cookies, then to an NSDictionary of headers, and finally, staple those headers to your request. This is comparable to doing it manually, as Adam Shiemke linked in the question errata, but much cleaner in my opinion.

As per the documentation, you may also want to check HTTPShouldHandleCookies to see if your default cookie policy is being used properly.

Community
  • 1
  • 1
MrGomez
  • 23,788
  • 45
  • 72
  • 1
    Works like a boss, but you should note that the OP must create an NSMutableURLRequest in order to be able to use setAllHTTPHeaderFields method. Simply change `NSURLRequest *request = [[NSURLRequest alloc] initWithURL:contentURL];` to `NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:contentURL];` – RobP Sep 14 '14 at 17:41
  • also, this seems like a great opportunity for a category, and a convenience method one could use along the lines of `[myMutableURLRequest addAllCookies]` – RobP Sep 14 '14 at 18:03
  • Awesome +1 @MrGomez – mkabatek Jun 08 '16 at 00:14
1

On iOS projects I found the ASIHTTPRequest very useful for this kind of problems. It does things like authentication and cookies a lot better that the build-in functions: http://allseeing-i.com/ASIHTTPRequest/

  • Thanks, I use this as well, but I couldn't find a way to make it work with Newsstand Kit. Do you know if they can work together? – Alex Petrov Apr 07 '12 at 16:57
  • Not sure, I haven't used this combination yet. You might find out more at http://allseeing-i.com/ASIHTTPRequest/How-to-use#persistent_cookies – Collin Kleine Apr 08 '12 at 07:52
  • 1
    ASIHTTPRequest is no longer supported by Ben Copsey who wrote it (last update May 2011) and he recommends not using it for new projects – joneswah Jan 31 '13 at 04:03
  • Yeah, I'm transferring all my projects to RestKit now :) – Alex Petrov Mar 13 '13 at 14:54