我有一个小型开源应用程序。
我使用 [NSString stringWithContentsOfURL]
下载网站的 html 并检查用户是否已在 Safari 中登录。
在 El Capitan 之前,一切工作正常:stringWithContentsOfURL
使用sharedCookiesStorage(Safari 的)并返回登录用户的页面 html。
但在 El Capitan 中,这种方式不起作用:stringWithContentsOfURL
返回用户未登录的页面的 html。因此它不再使用 Safari 的 cookie。 (我已在 Safari 中登录此页面)。
El Capitan 发生了一些变化,我必须将所有域名添加到 info.plist
中的 NSExceptionDomains
中,以便它可以从 http://获取数据,但我在最新的 OSX 版本中没有发现 stringWithContentsOfURL
有任何变化。
可能是什么问题?升级之前一切都很好。也许我可以以某种方式将 [NSHTTPCookieStorage sharedHTTPCookieStorage]
传递给 [NSString stringWithContentsOfURL]
?
获取 html 的实际代码是:
html = [NSString stringWithContentsOfURL:[NSURL URLWithString: [trackerSettDict objectForKey:@"loginCheckURL"]] encoding: NSUTF8StringEncoding error:&error];
一些测试错误的代码:
NSError *error;
NSString *html;
html = [NSString stringWithContentsOfURL:[NSURL URLWithString: @"https://google.com"] encoding: NSWindowsCP1251StringEncoding error:&error];
NSLog(@"%@", html);
最佳答案
在 El Capitan 中,每个应用程序都有自己的 cookie 存储。您可以通过[NSHTTPCookieStorage sharedCookieStorageForGroupContainerIdentifier:"Cookies"]
访问Safari的cookie存储。
关于xcode - El Capitan 中的 stringWithContentsOfURL cookie jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32921572/