给webview添加cookie

self.webView.scrollView.frame =self.webView.frame;
    _webView.backgroundColor = [UIColor grayColor];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height-20)];
    _webView.delegate = self;
    NSArray *headeringCookie = [NSHTTPCookie cookiesWithResponseHeaderFields:
                                [NSDictionary dictionaryWithObject:
                                 [[NSString alloc] initWithFormat:@"token=782e5f610c2c209e092d3d14cdf285851533534602732;"]
                                                            forKey:@"Set-Cookie"]
                                                                      forURL:[NSURL URLWithString:url2]];
//   NSDictionary *dic = [NSHTTPCookie requestHeaderFieldsWithCookies:headeringCookie];

   // NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:dic];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage]  setCookies:headeringCookie forURL:[NSURL URLWithString:url2] mainDocumentURL:[NSURL URLWithString:@"您的域名t"]];
    //发请求
    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",url2]]];
    _webView.scalesPageToFit = YES;
    [ self.webView loadRequest:req];
    [self.view addSubview:self.webView];

猜你喜欢

转载自blog.csdn.net/ping20/article/details/80427903