为了账号安全,请及时绑定邮箱和手机立即绑定

如何在UIWebView中显示身份验证质询?

如何在UIWebView中显示身份验证质询?

阿波罗的战车 2019-09-21 15:45:01
我正在尝试通过UIWebView访问安全的网站。当我通过野生动物园访问它时,我遇到了身份验证挑战,但是在应用程序的UIWebView中没有出现相同的问题。如何使其显示?任何指针,示例代码或链接都将非常有帮助。非常感谢。
查看完整描述

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;

}


查看完整回答
反对 回复 2019-09-21
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

如您所知,UIWebView不会提供与服务器通信的机会。我以这种方式解决了这个问题:在UIWebView的委托方法shouldStartLoadWithRequest中,我发起了另一个与NSURLConnection的连接,而在委托NSURLConnection的方法中,didReceiveAuthenticationChallenge已经处理了来自服务器的挑战。在方法didReceiveResponse中(如果挑战来了),然后再次在同一UIWebView中加载相同的URL(挑战已被处理:)。不要忘记在didReceiveResponse中取消连接,否则它将使流量增加一倍。


查看完整回答
反对 回复 2019-09-21
  • 3 回答
  • 0 关注
  • 424 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信