使用UIWebView共享WKWebView Cookies

时间:2017-04-01 09:11:08

标签: ios swift uiwebview wkwebview nshttpcookie

我尝试与UIWebView共享WKWebView Cookies以获取所有Cookie。我知道从UIWebView获取所有cookie比较WKWebView非常简单。

我在选项卡式应用程序模板中创建了两个WebView(WKWebView,UIWebView)。下面的方法,我用于与UIWebView共享WKWebView cookie,但没有取得成功。

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

当我从WKWebView登录我的帐户时使用上面的代码,UiWe​​bView没有登录我。 我还尝试与WKWebView共享UIWebView Cookies,它起作用了。

请有人告诉我如何与UIWebView共享WKWebView Cookies或如何从WKWebView获取所有cookie?

谢谢

2 个答案:

答案 0 :(得分:0)

您需要按照以下步骤设置wkwebview cookie

首先在初始化中执行脚本来设置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;
}

然后在创建NSURLRequest对象时手动将Cookie添加到HTTP的标头中:

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

答案 1 :(得分:0)

设置cookie和加载webview确实很痛苦,在加载webview之前设置cookie是很棘手的。如果仅在委托方法中设置cookie,则永远不会加载页面。您必须先做好Cookie的处理,然后再加载页面。您可以在其他问题中回答我的问题,但是很快,但是您可以在目标c中遵循相同的技术。 Can I set the cookies to be used by a WKWebView?