我使用下面提到的方法在WKWebview
設置Cookie: Can I set the cookies to be used by a WKWebView?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
- 請問
WKWebView
設置的Cookie回NSHTTPCookieStorage
在運行時(該方法在AJAX調用期間) - 我可以使用任何委託方法控制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;
}
注意,「sessionOnly」和「的isSecure」是在參數列表中錯誤的順序。 – gyimi
當您處理關鍵值對時,訂單真的很重要嗎?無論如何,我會嘗試它並在這裏評論.. –
Actuallty,它確實,因爲你沒有使用關鍵值對。您正在連接一個字符串。因此sessionOnly =%@; isSecure =%@應該在這種情況下讀作sessionOnly = + self.isSecure +; isSecure = + self.sessionOnly – gyimi