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

 

posted on 2017-06-22 17:43  sunyaxue  阅读(722)  评论(0编辑  收藏  举报

导航