Update WKWebView Cookie Values

China☆狼群 提交于 2020-01-03 05:26:17

问题


My Requirement is to update the cookies that are set to WKWebView. I'm able to update the cookies in NSHTTPCookieStorage, but the same cookies are not reflected to WKWebView cookies(The update values are present in NSHTTPCookieStorage but are not set to WKWebView)

The following is code that i have used to set that cookies to AJAX calls.

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]) {
    [cookieString appendString:[NSString stringWithFormat:@"%@;", cookie.getCookieString]];
    [script appendString:[NSString stringWithFormat:@"document.cookie='%@';",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:NO];
[userContentController removeAllUserScripts];

[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];

After updating the cookies, I tried to print the existing cookies in my NSHTTPCookieStorage, which shows the updated values.

NSLog(@"cookies:%@",[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);

I also tried printing the script in WKNavigationDelegate after updating the cookies, but it shows old values.

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"Script: %@",[wkWebView.configuration.userContentController.userScripts firstObject].source);

}

回答1:


The following code fixed my issue:

NSMutableString *script = [[NSMutableString alloc] init];
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    [script appendString:[NSString stringWithFormat:@"document.cookie='%@';",cookie.getCookieString]];
}
[wkwebView evaluateJavaScript:script completionHandler:^(id test, NSError *error){
    NSLog(@"test");
}];


-(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;
}


来源:https://stackoverflow.com/questions/38098567/update-wkwebview-cookie-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!