Objective c 未调用didReceiveAuthenticationChallenge

Objective c 未调用didReceiveAuthenticationChallenge,objective-c,ios,ipad,authentication,uiwebview,Objective C,Ios,Ipad,Authentication,Uiwebview,我有一个UIWebView进入一个有登录按钮的站点。一旦按下登录按钮(例如在Safari中),它会提示用户登录,然后调用通过AD进行身份验证的web服务并让用户登录。我正试图在我的UIWebView中实现这一点,但从未调用didReceiveAuthenticationChallenge。有什么想法吗 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigation

我有一个
UIWebView
进入一个有登录按钮的站点。一旦按下登录按钮(例如在Safari中),它会提示用户登录,然后调用通过AD进行身份验证的web服务并让用户登录。我正试图在我的
UIWebView
中实现这一点,但从未调用
didReceiveAuthenticationChallenge
。有什么想法吗

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    NSLog(@"Authed = %d", _authed);

    NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

    NSString *theURL = [[request URL] absoluteString];



    currentURL = [[request URL] absoluteString];

    if (!_authed) {
        _authed = NO;

        [[NSURLConnection alloc] initWithRequest:request delegate:self];
        NSLog(@"shouldStartLoadWithRequest Return NO");
        return NO;
    }

    NSLog(@"shouldStartLoadWithRequest Return YES");
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
    NSLog(@"got auth challange");

    NSLog(@"previousFailureCount = %d", [challenge previousFailureCount]);




    ch = challenge;


    [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"myusername" password:@"mypassword" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    NSLog(@"received response via nsurlconnection");

    _authed = YES;


    NSString *urladdress = currentURL;    
    NSURL *url = [NSURL URLWithString:urladdress];


    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    [itemWebView loadRequest:urlRequest];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
    return NO;
}

是否有一种方法可以手动调用didReceiveAuthenticationChallenge方法?

正如其他人所指出的,无法触发该方法的最有可能的解释是服务器没有返回错误消息。只有这样,UIWebView才会向其委托发送
didReceiveAuthenticationChallenge
消息

特别是,由于您将成功案例描述为“ntfs登录屏幕”——我假设您指的是Windows域登录对话框——它不是HTTP的一部分,所以听起来服务器使它看起来像登录屏幕,但它不是HTTP身份验证

需要考虑/尝试的事项:

  • 试着点击网址,确认你确实得到了401

  • 如果您没有得到401,那么您在桌面浏览器上看到的对话框要么是Javascript提示符,要么是一个HTML表单,其构造类似于401。尝试使用Firebug或Chrome's inspector等工具,找出真实凭证发布到的URL,然后在那里启动UIWebView

  • 如果你真的拿到了401,那么也许再检查一下你的代理连接?在这种情况下,应该对视图的委托调用此方法


最后:您询问是否可以手动触发
didReceiveAuthenticationChallenge
:当然,您可以将消息发送给您的学员,并包含一个质询,但这根本不是您想要的:这是您对学员的质询,而不是由服务器通过UIWebView进行验证。

该方法用于提供凭据以响应来自服务器的HTTP身份验证质询,而不是用于执行更典型的基于表单的身份验证。你确定该站点正在使用HTTP身份验证吗?嗨,John,是的,它不是基于表单的身份验证。当按下页面上的登录按钮(在Safari中)时,用户会收到ntfs登录屏幕的提示,该屏幕应该会触发该方法,但由于某些原因,它不是。