Search code examples
xcodecocoacookiesnsstringosx-elcapitan

stringWithContentsOfURL cookie jar in El Capitan


I have a small open source application. I used [NSString stringWithContentsOfURL] to download website's html and check if user has been logged in in Safari.

Before El Capitan everything was working fine: stringWithContentsOfURL used sharedCookiesStorage (Safari ones) and returned the html of the page as logged in user.

But in El Capitan this is not working this way: stringWithContentsOfURL returns the html of the page, where the user is not logged in. So it doesn't use Safari's cookies anymore. (I'm logged in on this page in Safari).

There were some changes in El Capitan, I had to add all my domains to NSExceptionDomains in info.plist, so it could get data from http://, but I haven't found any changes in stringWithContentsOfURL in the latest OSX release.

What can be the problem? Everything was fine before upgrading. Maybe I could somehow pass [NSHTTPCookieStorage sharedHTTPCookieStorage] to [NSString stringWithContentsOfURL]?

The actual code, that gets html is:

html = [NSString stringWithContentsOfURL:[NSURL URLWithString: [trackerSettDict objectForKey:@"loginCheckURL"]] encoding: NSUTF8StringEncoding error:&error];

Some code to test the error:

NSError *error;
NSString *html;
html = [NSString stringWithContentsOfURL:[NSURL URLWithString: @"https://google.com"] encoding: NSWindowsCP1251StringEncoding error:&error];
NSLog(@"%@", html);

Solution

  • In El Capitan, each app has its own cookie storage. You can access Safari's cookie storage by [NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:"Cookies"].