I had a requirement where i had to handle authentication of a site and load it in a UIWebview .After searching the net for while I created a new NSURLConnection and handled the authentication challenge(as in the link How to display the Authentication Challenge in UIWebView? ) using that
Everything works fine but i don't understand the following piece of code.
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSString *newUrl = [NSString stringWithFormat:@"%@", response.URL];
// cancel the connection. we got what we want from the response,
// no need to download the response data.
[connection cancel];
//Start loading the new request in webView
NSURL *url =[NSURL URLWithString:newUrl];
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
}
Here webview is simply loading the request again without resending any auth info in the cookies.Then i understood that iOS handles cookie management automatically.But when i tried to access the cookies and print it after the authentication,it gave nothing.I used the following piece of code to retrieve all the cookies.
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]
How does this work then?