0

For iOS, I have written a custom delegate and implementation of NSUrlSession. It is working fine for Http urls. But when I try to download image resources from a HTTPS source that has been authenticated and which require a Cookie to be passed, the download fails with a NSUrlErrorCode -1013 or -1002.

 NSMutableUrlRequest req = new NSMutableUrlRequest(new NSUrl(uri.ToString()));
 req.Headers = dictHeaders;//
 NSUrlSessionDownloadTask newTask = session.CreateDownloadTask(req);
 activeDownloads.Add(newTask);
 newTask.Resume();

So in the NSUrlSessionDownloadDelegate I have implemented the DidReceiveChallenge method but am not passing anything there currently because I want to pass the cookie from my initial login as above.

NSUrlSession uses shared session for the complete app but my initial login API is done using RestSharp.RestClient which fetches the cookie that I can use throughout the app.

So my question is how do I pass the cookie to NSUrlSession ?

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
B K
  • 1,554
  • 3
  • 19
  • 40

1 Answers1

0

As far as I know, all work with cookies goes through NSHTTPCookieStorage class https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookieStorage_Class/index.html. You can use cookieAcceptPolicy to configure how cookies are used in your application and even manually add/remove cookies if needed.

NSURLSession will take cookie from NSHTTPCookieStorage.

bealex
  • 10,004
  • 1
  • 21
  • 27
  • Yes that is what the document says and was the first thing I tried, but can we convert string (header value) to a NSHTTPCookieStorage object. – B K Apr 20 '15 at 05:41
  • You need to fill in all the cookie fields, at least, domain/name/value. WHat exactly string do you need to convert? – bealex Apr 20 '15 at 09:28