views:

1563

answers:

8

I am trying to access a secure website through UIWebView. When I access it through safari, i get an authentication challenge but the same does not appear in my UIWebView in the application. How can I make it appear?

Any pointers, sample code or links will be very helpful. Thanks a lot.

A: 

as you know, UIWebView does not provide opportunities to communicate with the server. I solved this problem this way: in the delegate method shouldStartLoadWithRequest of UIWebView I initiating another connection with NSURLConnection, and already in the method of the delegate NSURLConnection didReceiveAuthenticationChallenge processed the сhallenge from the server. Аnd in the method didReceiveResponse (if the challenge came) then again in the same UIWebView load the same URL (challenge has already been processed:). Do not forget to cancel connection in didReceiveResponse, otherwise it will double the traffic.

xzDeveloper
A: 

hi alexander, can I ask a sample code of yours? I am a newbie with regards to iPhone and I can not fully understand what you mean. Is it possible to get your sample code for the solution you presented?

My email address is [email protected]

Thank you very much.

Felix Gomez Jr
A: 

Could you show your didReceiveAuthenticationChallenge method please?

Shuriken
A: 

I implemented didReceiveAuthenticationChallenge at last! :) Now my NSURLConnection object passes authentication but UIWebView object doesn't pass authentication. Do you sure that web view must loaded with same URL?

Shuriken
+2  A: 

It's actually super easy... I'm sure you can just show a UIAlertView when the auth challenge delegate is shown (or prior to loading the URL, if you know for sure that the URL you're hitting will prompt for auth login info). Anyways, the trick is to create your own nsurlconnection and i do some logic to save weather the auth delegate has been used.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
       NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

       if (!_authed) {
               _authed = NO;
/* pretty sure i'm leaking here, leave me alone... i just happen to leak sometimes */
               [[NSURLConnection alloc] initWithRequest:request delegate:self];
               return NO;
       }

       return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
       NSLog(@"got auth challange");

       if ([challenge previousFailureCount] == 0) {
               _authed = YES;
/* SET YOUR credentials, i'm just hard coding them in, tweak as necessary */
               [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
       } else {
               [[challenge sender] cancelAuthenticationChallenge:challenge]; 
       }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
       NSLog(@"received response via nsurlconnection");

/** THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info **/

       NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:]];

       [_webView loadRequest:urlRequest];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
       return NO;
}
Sahil
Does not work with POSTs. Even a simple modification of this still does not work with POSTs.
Wayne Hartman
A: 

Hi, I would also like to know if Alexander could send me the sample code for this solution as I am having the exact same problem. Many thanks Jonty p.s.please send the source code here: [email protected]

Jean Van Iddekinge
A: 

This does not work, I'm using version 3.2 of iOS.

SoftmasterG
A: 

I'm trying to achieve the same thing, however it does not work for me with the code given by Sahil. The delegate methods are getting called, he seems to authenticate as well, but it does not load the given link into the webview after that. I try this on iPad with iOS 3.2, maybe that is the reason, i don't know.

any hints or tips will be very helpfull, thanks.

FlowDev