iOS webView 设置cookie

Posted 伊米伊念

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS webView 设置cookie相关的知识,希望对你有一定的参考价值。

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType中添加代码

    NSDictionary *dic = @{@"UserToken":[WCAppGlobal defaulAppGlobal].isLogin ? [self _getToken] : @""};
    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        // 设定 cookie
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                 [request.URL host], NSHTTPCookieDomain,
                                 [request.URL path], NSHTTPCookiePath,
                                 key,NSHTTPCookieName,
                                 obj,NSHTTPCookieValue,[[NSDate date] dateByAddingTimeInterval:60 * 60 * 24],NSHTTPCookieExpires,
                                 nil]];
        
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }];
 

 

以上是关于iOS webView 设置cookie的主要内容,如果未能解决你的问题,请参考以下文章

iOS webview 清除缓存

在iOS上反应Native WebView缺少会话cookie

小程序webview设置cookie延时很长

Cordova InAppBrowser 和与 webview 共享的 cookie

iOS UIWebView在HTML中丢失了cookie

将 cookie 设置为 WebView 控件