我最近通過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];
}
}
`
嗨,謝謝你的回覆。您能否向我解釋我將使用其他方法的順序? – d4ndym1k3 2011-05-15 06:47:30