我可以设置要由WKWebView使用的cookie吗? [英] Can I set the cookies to be used by a WKWebView?

查看:537
本文介绍了我可以设置要由WKWebView使用的cookie吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将现有的应用程序从UIWebView切换到WKWebView。当前应用程序管理Web视图之外的用户登录/会话,并将认证所需的Cookie设置到NSHTTPCookieStore中。不幸的是,新的WKWebView不使用来自NSHTTPCookieStorage的cookie。是否有其他方法来实现这一点?

I'm trying to switch an existing app from UIWebView to WKWebView. The current app manages the users login / session outside of the web view and sets the cookies required for authentication into the the NSHTTPCookieStore. Unfortunately new WKWebView doesn't use the cookies from the NSHTTPCookieStorage. Is there another way to achieve this?

推荐答案

如果您需要在初始加载请求中设置您的Cookie,他们对NSMutableURLRequest。因为cookie只是一个特殊格式化的请求头,所以可以这样实现:

If you require your cookies to be set on the initial load request, you can set them on NSMutableURLRequest. Because cookies are just a specially formatted request header this can be achieved like so:

WKWebView * webView = /*set up your webView*/
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/index.html"]];
[request addValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;" forHTTPHeaderField:@"Cookie"];
// use stringWithFormat: in the above line to inject your values programmatically
[webView loadRequest:request];

如果您要求网页上的后续AJAX请求设置Cookie,可以通过简单的在文档启动时使用WKUserScript通过javascript以编程方式设置值:

If you require subsequent AJAX requests on the page to have their cookies set, this can be achieved by simply using WKUserScript to set the values programmatically via javascript at document start like so:

WKUserContentController* userContentController = WKUserContentController.new;
WKUserScript * cookieScript = [[WKUserScript alloc] 
    initWithSource: @"document.cookie = 'TeskCookieKey1=TeskCookieValue1';document.cookie = 'TeskCookieKey2=TeskCookieValue2';"
    injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
// again, use stringWithFormat: in the above line to inject your values programmatically
[userContentController addUserScript:cookieScript];
WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
webViewConfig.userContentController = userContentController;
WKWebView * webView = [[WKWebView alloc] initWithFrame:CGRectMake(/*set your values*/) configuration:webViewConfig];

结合这两种技术应该能够提供足够的工具来将Cookie值从Native App Land传输到Web View Land 。您可以在 cookie javascript api on mozilla的页面上找到更多信息,如果您需要一些更高级的Cookie。

Combining these two techniques should give you enough tools to transfer cookie values from Native App Land to Web View Land. You can find more info on the cookie javascript api on mozilla's page, if you require some more advanced cookies.

是的,这很可笑,苹果不支持许多 UIWebView 。不知道他们会不会支持他们,但希望他们很快就会。希望这有助于!

Yeah it sucks that Apple is not supporting many of the niceties of UIWebView. Not sure if they will ever support them, but hopefully they will get on this soon. Hope this helps!

这篇关于我可以设置要由WKWebView使用的cookie吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆