私はWKWebview
にクッキーを設定するには、下記のアプローチを使用しています: Can I set the cookies to be used by a WKWebView?WKWebViewクッキー
しかし、私が設定したクッキーは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
がWKWebView
は、実行時にNSHTTPCookieStorage
(に戻ってクッキーを設定しますNSString
- としてクッキーの値を返すメソッドである:ここでは は、私が使用したコードスニペットですAJAXコール中)
- AJAXコールを任意のデリゲートメソッドで制御できますか?
ドメイン(またはパス)要求されたURLに一致する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;
}
注意を修正しました。 – gyimi
キー値ペアを扱っているときに、注文は本当に重要ですか?とにかく私はそれを試してここにコメントします。 –
Actuallty、それは、あなたがキーの値のペアを使用していないので、そうします。文字列を連結しています。したがって、sessionOnly =%@;この場合、sessionOnly = + self.isSecure +; isSecure = + self.sessionOnlyとしてisSecure =%@を読み取る必要があります。 – gyimi