I have a UIWebView
that loads a website that user Authentication. The site creates an Authentication cookie. When in the browser, unless you clear your cookies, you will always be logged in. When the xCODE loads it can see the cookie listed when looking at the cookie jar but it is not sent to the webView.
I would like to know how to make the webView aware that the auth cookie is there so it does not continue to prompt the user for authentication every single time.

- 3,976
- 9
- 30
- 39

- 221
- 1
- 3
- 5
3 Answers
You can use the NSURLConnection class to perform a HTTP request to login the website, and retrieve the cookie. To perform a request, just create an instance of NSURLConnection and assign a delegate object to it.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
Then, implement a delegate method.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSDictionary *fields = [HTTPResponse allHeaderFields];
NSString *cookie = [fields valueForKey:"Set-Cookie"]; // It is your cookie
}
Retain or copy the cookie string. When you want to perform another request, add it to your HTTP header of your NSURLRequest instance.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]];
[request addValue:cookie forHTTPHeaderField:"Cookie"];
TO delete Cookie anytime you can call this method:
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *each in [[[cookieStorage cookiesForURL:YOUR_URL] copy] autorelease]) {
[cookieStorage deleteCookie:each];
}
-
Ok, I'm new to xCODE and I have questions about this. Feel free to call me names if they are too simple. In the AppDelegate.m file I try to declare the NSURL and NSURLConnection and got a compiler error about those not beign constants. I implemented the delegate also in AppDelagate.m and that compiles. The NSMutable I have no idea in what method should that go. I'm sure this are simple questions but I really do not know. – Hector Jan 19 '12 at 18:54
You can read the authentication cookie from all websites using the shared cookie storage.
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies])
{
NSLog(@"%@", cookie);
}
OR to get the .net aspxauth cookie for your website
NSArray *cookiesForURL = [cookieJar cookiesForURL: [NSURL URLWithString: **MYURL**]];
for (cookie in cookiesForURL)
{
if([cookie.name compare:@".ASPXAUTH"] == NSOrderedSame)
{
NSLog(@"%@", cookie);
break;
}
}

- 121
- 1
- 3
I solved the issue. The problem has to do with configuration as the UIWebView is not getting seen as a standard web browser. I'm authenticating user through .net and IIS. I added a generic.browser file in the App_Browser directory of my website. The content of the file is:
<browsers>
<browser refID="Mozilla" >
<capabilities>
<capability name="cookies" value="true" />
</capabilities>
</browser>
<browser refID="Default">
<capabilities>
<capability name="cookies" value="true" />
</capabilities>
</browser>
</browsers>
The line that corrects the issue is the capability allowing the cookies.
The reference to the solution I used was this