htmlioscookiessavensurlconnectiondelegate

After stringWithContentsOfURL cookies not save in UIWebView. iOS


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


Solution

  • 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!