2

I want get html from url and I use:

NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];

But after this my cookies clean in UIWebView. But if i use load request without stringWithContentsOfURL cookies save:

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

I have tried it but cookies not save too:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

How do I get a HTML and load it in UIWebView with cookies?

UPDATE: If i use this case (two unrelated lines) cookies not save too:

NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

i.e. stringWithContentsOfURL does not save cookies. How can this be? it's funny :D

runia
  • 370
  • 6
  • 19

2 Answers2

0

In this case - you download raw content from URL, and just showing this data in WebView. WebView just don't know about your cookies.

NSString *URLtoHTML = [NSString stringWithContentsOfURL:self.url encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:URLtoHTML baseURL:nil];

In this case - you create connection, but once again, download only raw data.

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

This one is closer, all you need - it's capture loaded data from WebView.

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
NSString *html = [self.webView stringByEvaluatingJavaScriptFromString: 
                                     @"document.body.innerHTML"];
Taier
  • 2,109
  • 12
  • 22
  • But in this case I have to wait for the page is loaded: -(void)webViewDidFinishLoad:(UIWebView *)webView { NSString *html = [self.webView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"]; } It's a long time and is not suitable for me. There are any suggestions? – runia Mar 01 '15 at 20:46
0

MY SOLUTION:

First I checked the Set-Cookie which I get from the server:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSDictionary *fields = [HTTPResponse allHeaderFields];
    NSString *cookie = [fields valueForKey:@"Set-Cookie"]; // It is cookie
}

And I realized that I come value, in which cookies are disabled. Now I first sends a request with cookies enabled:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.simple.com/"]];
[request addValue:myCustomCookies forHTTPHeaderField:@"Cookie"];

MY SOLUTION 2:

I send request with post data

NSString *post = [NSString stringWithFormat:@"login=%@&passwors=%@",_login.text,_password.text];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.simple.com/login"]]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];
        request.timeoutInterval = 20;
        NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

That's all! Good luck to everyone!

runia
  • 370
  • 6
  • 19