WKWebView與Cookie

原生與頁面交互,在其中一邊登錄過了,另外一邊需要用cookie來驗證。

如果是要整個WKWebView需要cookie則用這個方法從NSHTTPCookieStorage中拿到一開始存儲到是cookie

 WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
    // 設置偏好設置
    webConfig.preferences = [[WKPreferences alloc] init];
    // 默認為0
    webConfig.preferences.minimumFontSize = 10;
    // 默認認為YES
    webConfig.preferences.javaScriptEnabled = YES;
    // 在iOS上默認為NO,表示不能自動通過窗口打開
    webConfig.preferences.javaScriptCanOpenWindowsAutomatically = NO;
    
    // web內(nèi)容處理池
    webConfig.processPool = [[WKProcessPool alloc] init];
    // 將所有cookie以document.cookie = 'key=value';形式進行拼接

    /* **********在此處獲取返回的cookie********* */
    NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
    NSMutableString *cookieValue = [NSMutableString stringWithFormat:@""];
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        [cookieDic setObject:cookie.value forKey:cookie.name];
    }
    
    // cookie重復,先放到字典進行去重,再進行拼接
    for (NSString *key in cookieDic) {
        NSString *appendString = [NSString stringWithFormat:@"document.cookie = '%@=%@';", key, [cookieDic valueForKey:key]];
        [cookieValue appendString:appendString];
    }
    
//    NSString *cookieValue = @"document.cookie = 'fromapp=ios';document.cookie = 'channel=appstore';";
    
    // 加cookie給h5識別,表明在ios端打開該地址
    WKUserContentController* userContentController = WKUserContentController.new;
    WKUserScript * cookieScript = [[WKUserScript alloc]
                                   initWithSource: cookieValue
                                   injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    [userContentController addUserScript:cookieScript];
    webConfig.userContentController = userContentController;
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) configuration:webConfig];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;

如果是只是單個頁面URL需要cookie則用這個方法從NSHTTPCookieStorage中拿到一開始存儲到是cookie

   NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];
    NSMutableString *cookieValue = [NSMutableString stringWithFormat:@""];
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        [cookieDic setObject:cookie.value forKey:cookie.name];
    }
    
    // cookie重復,先放到字典進行去重,再進行拼接
    for (NSString *key in cookieDic) {
        NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];
        [cookieValue appendString:appendString];
    }
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:WeiXiShanHuoLink]];
    [request addValue:cookieValue forHTTPHeaderField:@"Cookie"];

    [_webView loadRequest:request];
    [self.view addSubview:_webView];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容