Share WKWebView Cookies With UIWebView

烂漫一生 提交于 2019-12-25 12:47:05

问题


I'm trying to share WKWebView Cookies with UIWebView for getting all cookies. I know its very simple to get all cookies from UIWebView as compare WKWebView.

I create two WebView's (WKWebView, UIWebView) in Tabbed Application Template. Below method that i am using for share WKWebView cookies with UIWebView but didn't get success.

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

     let response = navigationResponse.response as? HTTPURLResponse
     let cookies = HTTPCookie.cookies(withResponseHeaderFields: response?.allHeaderFields as! [String : String], for: (response?.url)!)

     HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always

     for cookie in cookies {
     HTTPCookieStorage.shared.setCookie(cookie)
     }

    decisionHandler(WKNavigationResponsePolicy.allow);
 }

Using above code when i Login Into my account from WKWebView, UiWebView didn't Login me already. I also tried to share UIWebView Cookies With WKWebView, and it worked.

Please can anyone tell me how i can share WKWebView Cookies With UIWebView or how i can get all cookies from WKWebView?

Thanks


回答1:


You need to set the wkwebview cookies by following the steps below

first in the initialization is to execute the script to set up Cookies:

-(void)initWebView
{
    WKWebViewConfiguration *webViewconfiguration = [[WKWebViewConfiguration alloc] init];
    WKUserContentController *wkUController = [[WKUserContentController alloc] init];
    if(URL.host hasSuffix:baseDomain){
    //Here to determine whether the domain name is their own website
        NSString *jScript = [self setCookies];
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
                    [wkUController addUserScript:wkUScript];
}
    webViewconfiguration.userContentController = wkUController;
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, width, height) configuration:webViewconfiguration];
}
//The script that executes may be slightly different
+(NSString *)setCookies
{
     NSString *script = [NSString string];

    for (NSHTTPCookie *httpCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
    {
       NSString *cookie = [NSString stringWithFormat:@"%@=%@;domain=%@;path=%@",httpCookie.name,httpCookie.value,httpCookie.domain,httpCookie.path?:@"/"];
       script = [script stringByAppendingString:[NSString stringWithFormat:@"document.cookie='%@';",cookie]];

    }
    return script;
}

And then manually add cookies to the header of the HTTP when creating the NSURLRequest object:

- (void)loadRequest:(NSURLRequest *)request
{
    //Here to determine whether the domain name is their own website
    if ([request.URL.host hasSuffix:baseDomain]){
        NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:request.URL];
        NSString *cookies = @""; //setting Cookies
        [mutableRequest addValue:cookies forHTTPHeaderField:@"Cookie"];
}
    // do request  
}



回答2:


This is really painful to setup cookies and load webview, it's tricky to set cookies before loading webview. if you set cookies in just a delegate method it never load page first time. you must take care of cookies are done before loading page. you can ceck my answer in other question, but it is in swift but you can follow the same technique in objective c. Can I set the cookies to be used by a WKWebView?



来源:https://stackoverflow.com/questions/43154918/share-wkwebview-cookies-with-uiwebview

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