0

I'm having trouble on an iOS app using NSURLSession to login into a website that uses cookies to record successful authentication.

Using the defaultSessionConfiguration() I have no problem. (It even works when the app is in the background and triggered via a Remote Push message.)

When I configure the session to use backgroundSessionConfigurationWithIdentifier() then I can authenticate but the appropriate cookies are not present for subsequent downloads.

The use of backgroundSession fails when the app is in the foreground and the background.

So there must be a difference between default and background that is affecting the cookies. Both configurations are documented as using the shared cookie storage.

Does anybody have an explanation and/or fix for this behavior??

andymc
  • 51
  • 7

1 Answers1

0

There are a few reasons for this. One of them is because you have not enabled the setting for background fetching in your .plist. Another is the need to persist a session with identifiers across the app, which is one reason why your cookies don't show up in subsequent downloads, because something is getting switched up.

defaultSessionConfiguration works as you would expect it to and you are used to, but backgroundSessionConfiguration is quite different, and you will need to set up a few things.

I would recommend reading through this to get some information on how it differs and what you can do: https://medium.com/swift-programming/learn-nsurlsession-using-swift-part-2-background-download-863426842e21#.vejklegsp

I hope this is helpful!

dokun1
  • 2,120
  • 19
  • 37
  • Thanks for your suggestions. – andymc Mar 28 '16 at 23:08
  • Thanks for the ideas @dokun1 . I am using the same session for all requests. I have background fetching enabled - hence I can complete the login authentication and requests pages. My question remains: **What is the difference between default and background sessions??** My app is fully functional - including in the background - using the default config. The docs say to use the background config when doing background operations. When I use the background config, I can't authenticate with my server. So, what is the **real difference** between these two configs? – andymc Mar 28 '16 at 23:18