开发者

iphone uiwebview authentication challenge keeps firing when signed in

开发者 https://www.devze.com 2023-03-06 11:29 出处:网络
i have recently implemented authentication challenge log in through the iPhones UIWebView. i\'ve got it working to the point where i get challenged then i present an alert with text fields, then send

i have recently implemented authentication challenge log in through the iPhones UIWebView. i've got it working to the point where i get challenged then i present an alert with text fields, then send the data to the site that needs authentication.

i have not yet tried to use this on anything else besides my netgear router. But my problem is when i navigate through the settings for the router, the authentication challenge gets called, thus presenting the alert even though the user is logged in.

below is the code i'm using, any advice would be grately appreciated

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

if (_authed) {
    NSLog(@"_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 YES;
}
[[NSURLConnection alloc] initWithRequest:request delegate:self];
return YES;}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"protection space %@", protectionSpace.authenticationMethod);
//if(![protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault]){
return NO;
//}
//else{
//  return YES;
//}

//[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic];}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;{开发者_如何转开发NSLog(@"received response via nsurlconnection %@", connection);

NSLog(@"got auth challange %@", challenge);
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
/*NSString *aarrgghh=[NSString stringWithFormat:@"%@",connection];
NSString *searchForMe = @"login";
NSLog (@"arrgghhh %@",aarrgghh);
NSRange range = [aarrgghh rangeOfString:searchForMe];*/


if ([challenge previousFailureCount] <=1) {

    //present alert with text fields for credentials
} else {
    [[challenge sender] cancelAuthenticationChallenge:challenge]; 
}}

-(void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
NSLog(@"Challenge cancelled");}

//`-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"received data"); }

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

    // THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info 
    if(_authed){
    //NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webView.request.URL.absoluteString]];

    [webView loadRequest:myRequest];
}
}

`


Might be a simpler way to do this, but this is what worked for me.

First off, when shouldStartLoadWithRequest returns YES, that tells UIWebView to create NSURLConnections and run them for you . Since you can't assign a delegate to this connection, that's not going to work. If you want to handle authentication via a NSURLConnectionDelegate, then shouldStartLoadWithRequest should always return NO for that UIWebView.

So you need to handle the connection yourself. Fire off an NSURLConnection with the request and use the rest of the NSURLConnection delegate methods to handle the loading (e.g. keep track of the MIME type and build up an NSMutableData)

Finally, when you get to connectionDidFinishLoading, you can call UIWebView's loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL with the NSData your connection downloaded.

0

精彩评论

暂无评论...
验证码 换一张
取 消