WKWebView Cookies [英] WKWebView Cookies

查看:241
本文介绍了WKWebView Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面提到的方法在 WKWebview 中设置Cookie:
我可以设置WKWebView使用的cookie吗?

I'm using the below mentioned approach to set cookies in a WKWebview: Can I set the cookies to be used by a WKWebView?

但是我设置的cookie在AJAX调用中被复制了。我的意思是他们被重复了两次。

But the cookies that I have set are being duplicated in the AJAX calls. I mean they are being repeated twice.

这是我使用的代码片段:

Here is the code snippet that I used:

NSString *strURL = DASHBOARDURL;    
NSURL *url = [NSURL URLWithString:strURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];

NSMutableString *script = [[NSMutableString alloc] init];
NSMutableString *cookieString = [[NSMutableString alloc] init];

for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    [script appendString:[NSString stringWithFormat:@"document.cookie='%@';",cookie.getCookieString]];
    [cookieString appendString:[NSString stringWithFormat:@"%@;", cookie.getCookieString]];
}
[request setValue:cookieString forHTTPHeaderField:@"Cookie"];

//cookies for further AJAX calls
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
WKUserScript *cookieInScript = [[WKUserScript alloc] initWithSource:script
                                                      injectionTime:WKUserScriptInjectionTimeAtDocumentStart
                                                   forMainFrameOnly:YES];
[userContentController addUserScript:cookieInScript];

WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init];
webViewConfig.userContentController = userContentController;

CGRect viewRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);

wkWebview = [[WKWebView alloc] initWithFrame:viewRect configuration:webViewConfig];
wkWebview.navigationDelegate = self;
[wkWebview loadRequest:request];
[self.view addSubview:wkWebview];

getCookieString 是一个返回cookie值的方法作为 NSString

getCookieString is a method that returns cookie values as an NSString


  1. 请问 WKWebView 在运行时(在AJAX调用期间)将cookie设置回 NSHTTPCookieStorage

  2. 我可以控制AJAX调用使用任何委托方法的cookie?

  1. Will the WKWebView set the cookies back to NSHTTPCookieStorage at runtime(During AJAX calls)
  2. Can i control AJAX calls cookies with any delegate methods?

以下是我的 getCookieString 类别( NSHTTPCookie(CookieObject))方法

The following is my getCookieString category(NSHTTPCookie (CookieObject)) method

- (NSString *)getCookieString {

  NSString *string = [NSString stringWithFormat:@"%@=%@;domain=%@;expiresDate=%@;path=%@;sessionOnly=%@;isSecure=%@",
                    self.name,
                    self.value,
                    self.domain,
                    self.expiresDate,
                    self.path ?: @"/",
                    self.isSecure ? @"TRUE":@"FALSE",
                    self.sessionOnly ? @"TRUE":@"FALSE"];

  return string;
}


推荐答案

如果有,会发送多个Cookie Cookie存储区中的多个Cookie,其域(或路径)与请求的URL匹配。

Multiple Cookies are sent if there are more than one cookie in the cookie store whose domains (or paths) match the requested URL.

同时编写 getCookieString 您可能已更改或添加 domain = 字符串部分的方法。这将导致存储第二个有效的cookie,并随之与您的请求一起发送。

Whilst writing your getCookieString method you may have changed or added the domain= part of the string. This would cause a second valid cookie to be stored and therefor sent with your request.

这篇关于WKWebView Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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