gpt4 book ai didi

IOS UIWebView 添加 Cookie 值请求

转载 作者:行者123 更新时间:2023-11-29 01:04:24 26 4
gpt4 key购买 nike

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

最佳答案

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

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

}

关于IOS UIWebView 添加 Cookie 值请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36577427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com