Reputation: 301
I've got a UIWebView
going to a site that has a login button. Once the login button is pressed (for example in Safari), it prompts for a login, then calls a web service that authenticates via AD and logs the user in. I'm trying to get this to work in my UIWebView
, but the didReceiveAuthenticationChallenge
never gets called. Any ideas?
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Authed = %d", _authed);
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);
NSString *theURL = [[request URL] absoluteString];
currentURL = [[request URL] absoluteString];
if (!_authed) {
_authed = NO;
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"shouldStartLoadWithRequest Return NO");
return NO;
}
NSLog(@"shouldStartLoadWithRequest Return YES");
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"got auth challange");
NSLog(@"previousFailureCount = %d", [challenge previousFailureCount]);
ch = challenge;
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"myusername" password:@"mypassword" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"received response via nsurlconnection");
_authed = YES;
NSString *urladdress = currentURL;
NSURL *url = [NSURL URLWithString:urladdress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[itemWebView loadRequest:urlRequest];
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
return NO;
}
Is there a way to maybe manually call the didReceiveAuthenticationChallenge
method?
Upvotes: 3
Views: 7199
Reputation: 4791
As others have noted, the most likely explanation for why you're not getting that method to trigger is that the server is not returning an HTTP 401 Unauthorized
. Only then will the UIWebView send it's delegate the didReceiveAuthenticationChallenge
message.
Particularly since you describe the successful case as an "ntfs login screen" -- by which I'm assuming you mean a Windows domain logon dialog -- which is not part of HTTP, it sounds like server makes it look like a login screen, but it's not HTTP Auth.
Things to consider/try:
Try hitting the URL with cURL and confirm that you are indeed getting a 401.
If you're not getting a 401, then the dialog you see on your desktop browser is either a Javascript prompt or an HTML form constructed to look like one. Try using tools like Firebug or Chrome's inspector to figure out what URL the real credentials are posted to, and start your UIWebView there.
If you are in fact getting a 401, then perhaps double check your delegate connections? In that case, this method should be called on the view's delegate.
Finally: you ask if you could fire didReceiveAuthenticationChallenge
manually: of course you could send the message to your delegate, and include a challenge, but that's not at all what you want: that's you challenging the delegate, rather than the server doing it by your UIWebView.
Upvotes: 3