gpt4 book ai didi

ios - WKWebView Cookies

转载 作者:行者123 更新时间:2023-11-29 11:58:59 25 4
gpt4 key购买 nike

我正在使用下面提到的方法在 WKWebview 中设置 cookie: Can I set the cookies to be used by a WKWebView?

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

这是我使用的代码片段:

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

返回的方法
  1. WKWebView 会将 cookie 设置回 NSHTTPCookieStorage在运行时(在 AJAX 调用期间)
  2. 我可以使用任何委托(delegate)方法控制 AJAX 调用 cookie 吗?

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

- (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 的域(或路径)与请求的 URL 匹配,则发送多个 cookie。

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

关于ios - WKWebView Cookies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37856891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com