iphone uiwebview身份验证挑战在登录时始终触发
我最近通过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];
}
}
`
可能做到这一点更简单的方法,但这对我来说是什么工作。
首先,当shouldStartLoadWithRequest
返回YES,它告诉UIWebView创建NSURLConnection
并为您运行它们。既然你不能分配一个委托给这个连接,这是行不通的。如果你想通过NSURLConnectionDelegate
来处理认证,那么shouldStartLoadWithRequest
应该总是为该UIWebView返回NO。
所以你需要自己处理连接。关火与请求的NSURLConnection
和使用的NSURLConnection
委托方法剩下来处理负载(例如保持MIME类型的轨道,并建立了一个NSMutableData
)
最后,当你到connectionDidFinishLoading
,您可以拨打UIWebView
的loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL
与NSData
您的连接已下载。
嗨,谢谢你的回复。您能否向我解释我将使用其他方法的顺序? – d4ndym1k3 2011-05-15 06:47:30