2016-06-16 164 views
1

我使用下面提到的方法在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

  1. 請問WKWebView設置的Cookie回NSHTTPCookieStorage 在運行時(該方法在AJAX調用期間)
  2. 我可以使用任何委託方法控制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; 
} 
+0

注意,「sessionOnly」和「的isSecure」是在參數列表中錯誤的順序。 – gyimi

+0

當您處理關鍵值對時,訂單真的很重要嗎?無論如何,我會嘗試它並在這裏評論.. –

+0

Actuallty,它確實,因爲你沒有使用關鍵值對。您正在連接一個字符串。因此sessionOnly =%@; isSecure =%@應該在這種情況下讀作sessionOnly = + self.isSecure +; isSecure = + self.sessionOnly – gyimi

回答

1

多個cookie被髮送,如果有在cookie存儲其域(或路徑)匹配請求的URL多​​個cookie。

雖然編寫您的getCookieString方法,您可能已更改或添加domain=字符串的一部分。這將導致第二個有效的cookie被存儲並隨之發送您的請求。

+0

完美答案..謝謝 –

0

刪除domain=從我getCookieString方法解決了該問題

-(NSString *)getCookieString 
{ 
    NSString *string = [NSString stringWithFormat:@"%@=%@;expiresDate=%@;path=%@;sessionOnly=%@;isSecure=%@", 
      self.name, 
      self.value, 
      self.expiresDate, 
      self.path ?: @"/", 
      self.isSecure ? @"TRUE":@"FALSE", 
      self.sessionOnly ? @"TRUE":@"FALSE"]; 
    return string; 
}