sharedContainerIdentifier on NSURLConnection?

169 Views Asked by At

I'm adding a Widget extension to an iOS app. The Widget needs to make network calls, sharing the cookies set during the main app's login process. The NSURLConfiguration's sharedContainerIdentifier is perfect for this. Since we're already using App Groups this was easy to implement.

The main app is mostly on NSURLSession, but has some old NSURLConnection code. Does a mechanism exist to use NSURLConnection with a sharedContainerIdentifier?

1

There are 1 best solutions below

0
dgatwood On

I can think of three ways:

  1. You can use +[NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:] to obtain the shared cookie store, then manage cookies yourself using a custom NSURLProtocol sublass registered in the shared (global) protocol namespace. In that custom protocol, extract any new cookies from responses and inject any matching cookies into requests. Then tag the requests (so you don't process them twice) and pass them back into the NSURLConnection machinery.

  2. You can do the above, but instead of managing cookies in the shared cookie jar, use NSURLSession to issue the requests in a session configured with your shared container identifier.

  3. You can swizzle the NSURLConnection methods and replace them with NSURLSession calls.

None of these approaches is likely to be easier than modernizing the code and converting it to use NSURLSession unless there are third-party closed-source libraries involved.