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?
I can think of three ways:
You can use
+[NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:]to obtain the shared cookie store, then manage cookies yourself using a customNSURLProtocolsublass 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 theNSURLConnectionmachinery.You can do the above, but instead of managing cookies in the shared cookie jar, use
NSURLSessionto issue the requests in a session configured with your shared container identifier.You can swizzle the
NSURLConnectionmethods and replace them withNSURLSessioncalls.None of these approaches is likely to be easier than modernizing the code and converting it to use
NSURLSessionunless there are third-party closed-source libraries involved.