【发布时间】:2016-04-12 15:15:59
【问题描述】:
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"];
}
【问题讨论】:
标签: ios objective-c cookies uiwebview