Search code examples
macoscocoaosx-elcapitan

NSHTTPCookieStorage sharedHTTPCookieStorage comes up empty in 10.11?


Can anyone else verify this issue in 10.11?

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];  
NSLog(@"cookieStorage count is %lu", [[cookieStorage cookies] count]);  

When I run this in El Capitan, cookieStorage comes up (null) where I get the complete set of cookies in 10.10 and earlier. Is anyone else doing anything with cookies in 10.11 - this seems like a pretty significant bug?

EDIT with answer:

Not (yet) documented in the NSHTTPCookieStorage class reference is the new method for 10.11:

//access to Safari cookie storage
[NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:@"Cookies"]

Each application has its own cookie storage in 10.11. With thanks to the answer to this question: stringWithContentsOfURL cookie jar in El Capitan

Some more info in the WWDC 15 session on Networking with NSURLSession.


Solution

  • It turns out 10.11 to sandbox every individual app like iOS. Here is my solution.

     // To read cookies from Safari
    NSHTTPCookieStorage *storage  = [NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:@"Cookies"];
    
     // To write cookies to the app
    for (NSHTTPCookie *aCookies in [storage cookies]){
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:aCookies];
    }
    

    Hope this would be helpful.