xcode - El Capitan 中的 stringWithContentsOfURL cookie jar

标签 xcode cocoa cookies nsstring osx-elcapitan

我有一个小型开源应用程序。 我使用 [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/

相关文章:

objective-c - 隐藏 NSWindow 标题栏

ios - 正确管理addObserverForName :object:queue:usingBlock:

cocoa - 如何发送路径控件选择文件夹提示值?

node.js - 我可以将 cookie 添加到 webpack 开发服务器代理吗?

c++ - 找不到 -lopencv_highgui.2.4.9 的库

ios - swift 4 : Adding dictionaries to Plist

swiftui - 如何避免 'Unable to simultaneously satisfy constraints' 错误?

java - 如何在 Java 中手动将 cookie 添加到 Web 服务调用中?

jquery - 如何在 Rails 中的 JSON 响应中包含 cookie?

xcode - swift 中的 lldb 错误