IOS UIWebView添加Cookie值来请求

时间:2016-04-12 15:15:59

标签: ios objective-c cookies uiwebview

Xcode的新手,我想将页面加载到UIWebView中,但是设置了一个带有请求的cookie。我可以NSLog的cookie值,所以我知道它的设置。

这就是我现在正在做的事情:

-(IBAction)displayRedZones:(id)sender
{
   NSLog(@"red zones");
   UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(25/100 * (float)self.view.bounds.size.width, 25/100 * (float)self.view.bounds.size.height, (float)self.view.bounds.size.width / 2, (float)self.view.bounds.size.height / 2)];

   NSString* combinedString = [self.urlToLoad stringByAppendingString:@"/redzone"];
   NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: combinedString] cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 1];


    [request setHTTPShouldHandleCookies:YES];

    webView.scalesPageToFit = YES;
    webView.delegate = self;
    [self addCookies:cookies forRequest:request];
    [webView loadRequest: request];
    [self.view addSubview:webView];  
}

- (void)addCookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request
{
    NSString *cookieHeader = nil;
    NSString* cookieValue = [@"CAKEPHP" stringByAppendingString:self.cookieValue];
    [request setValue:cookieHeader forHTTPHeaderField:@"Cookie"];
}

1 个答案:

答案 0 :(得分:1)

我通过执行以下操作解决了这个问题:

-(IBAction)displayRedZones:(id)sender
{
NSLog(@"red zones");
// Create a web view that fills the entire window, minus the toolbar height


UIWebView *webView = [[UIWebView alloc]
                      initWithFrame:CGRectMake(0, 0, (float)self.view.bounds.size.width,
                                               (float)self.view.bounds.size.height)];
webView.tag=55;

NSString* combinedString = [self.urlToLoad stringByAppendingString:@"/redzone"];

NSLog(@"requestURL : %@",combinedString);
NSURL *url = [[NSURL alloc] initWithString:combinedString];
NSMutableURLRequest *request;


request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

ontracHTTPModel *model = [ontracHTTPModel sharedManager];

if (![model.cookieAuth isEqualToString:@""]) {
    NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"MYCOOKIE", NSHTTPCookieName,
                                      @"mydomain.com", NSHTTPCookieDomain,
                                      model.cookieValue, NSHTTPCookieValue,
                                      @"/", NSHTTPCookiePath,
                                      nil];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    NSArray* cookieArray = [NSArray arrayWithObjects: cookie, nil];
    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
    [request setAllHTTPHeaderFields:headers];
}
NSError *error;


NSString *webData= [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];


webView.scalesPageToFit = YES;
webView.delegate = self;
[webView loadHTMLString:webData baseURL:nil];
[self.view addSubview:webView];

}