2011-05-14 93 views
0

我最近通過iPhones UIWebView實現了身份驗證挑戰登錄。我已經把它工作到了我受到挑戰的地步,然後我提出了一個帶有文本字段的提醒,然後將數據發送到需要身份驗證的站點。iphone uiwebview身份驗證挑戰在登錄時始終觸發

我還沒有嘗試過使用這個除了我的netgear路由器之外的其他東西。但我的問題是,當我瀏覽路由器的設置時,認證挑戰被調用,因此即使用戶登錄也會顯示警報。

下面是我使用的代碼,任何建議都會感激理解

- (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");} 

//` - (無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據{ 的NSLog(@ 「接收的數據」); }

- (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]; 
} 
} 

`

回答

0

可能做到這一點更簡單的方法,但這對我來說是什麼工作。

首先,當shouldStartLoadWithRequest返回YES,它告訴UIWebView創建NSURLConnection併爲您運行它們。既然你不能分配一個委託給這個連接,這是行不通的。如果你想通過NSURLConnectionDelegate來處理認證,那麼shouldStartLoadWithRequest應該總是爲該UIWebView返回NO。

所以你需要自己處理連接。關火與請求的NSURLConnection和使用的NSURLConnection委託方法剩下來處理負載(例如保持MIME類型的軌道,並建立了一個NSMutableData

最後,當你到connectionDidFinishLoading,您可以撥打UIWebViewloadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURLNSData您的連接已下載。

+0

嗨,謝謝你的回覆。您能否向我解釋我將使用其他方法的順序? – d4ndym1k3 2011-05-15 06:47:30