3 回答

TA贡献1824条经验 获得超5个赞
实际上,这非常容易...我确定您可以在显示auth挑战委托时(或在加载URL之前,如果您确定所击中的URL会提示您输入auth登录信息,则只需显示UIAlertView)即可。 )。无论如何,诀窍是创建自己的方法NSURLConnection,我做一些逻辑来保存是否使用了auth委托。
- (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;
}

TA贡献1871条经验 获得超13个赞
如您所知,UIWebView不会提供与服务器通信的机会。我以这种方式解决了这个问题:在UIWebView的委托方法shouldStartLoadWithRequest中,我发起了另一个与NSURLConnection的连接,而在委托NSURLConnection的方法中,didReceiveAuthenticationChallenge已经处理了来自服务器的挑战。在方法didReceiveResponse中(如果挑战来了),然后再次在同一UIWebView中加载相同的URL(挑战已被处理:)。不要忘记在didReceiveResponse中取消连接,否则它将使流量增加一倍。
- 3 回答
- 0 关注
- 450 浏览
相关问题推荐
添加回答
举报