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

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

问题描述

我正在尝试将现有应用从 UIWebView 切换到 WKWebView.当前应用程序在 webview 之外管理用户登录/会话,并将身份验证所需的 cookies 设置到 NSHTTPCookieStore 中.不幸的是,新的 WKWebView 不使用 NSHTTPCookieStorage 中的 cookies.还有其他方法可以实现吗?

I'm trying to switch an existing app from UIWebView to WKWebView. The current app manages the users login / session outside of the webview 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?

推荐答案

仅适用于 iOS 11+ 编辑

使用 WKHTTPCookieStore:

let cookie = HTTPCookie(properties: [
    .domain: "example.com",
    .path: "/",
    .name: "MyCookieName",
    .value: "MyCookieValue",
    .secure: "TRUE",
    .expires: NSDate(timeIntervalSinceNow: 31556926)
])! 

webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)

由于您是从 HTTPCookeStorage 中提取它们,因此您可以这样做:

Since you are pulling them over from HTTPCookeStorage, you can do this:

let cookies = HTTPCookieStorage.shared.cookies ?? []
for cookie in cookies {
    webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
}

iOS 10 及更低版本的旧答案

如果您需要在初始加载请求上设置 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.您可以在 Mozilla 页面上的 cookie javascript API 上找到更多信息 如果您需要一些更高级的 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.

是的,Apple 不支持许多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天全站免登陆