2016-04-12 10 views
0

Xcodeに新しく、ページをUIWebViewに読み込みたいが、リクエストでクッキーを設定したい。私はそれが設定されていることを知っているので、NSLogのクッキー値をすることができます。ここでIOS UIWebViewリクエストに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

私は、次の操作を行って、この問題を解決:

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

} 
関連する問題