objective-c - didReceiveAuthenticationChallenge 未被调用

标签 objective-c ios ipad authentication uiwebview

我有一个 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方法?

最佳答案

正如其他人所指出的,对于为什么您没有触发该方法的最可能的解释是服务器没有返回 HTTP 401 Unauthorized。 .只有这样,UIWebView 才会向其委托(delegate)发送 didReceiveAuthenticationChallenge 消息。

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

需要考虑/尝试的事情:

  • 尝试使用 cURL 访问 URL并确认您确实收到了 401。

  • 如果您没有收到 401,那么您在桌面浏览器上看到的对话框要么是一个 Javascript 提示,要么是一个看起来像 Javascript 的 HTML 表单。尝试使用 Firebug 或 Chrome 的检查器等工具来确定将真实凭据发布到哪个 URL,并在那里启动你的 UIWebView。

  • 如果您实际上得到了 401,那么也许仔细检查您的委托(delegate)连接?在这种情况下,应在 View 的委托(delegate)上调用此方法。

最后:您询问是否可以手动触发 didReceiveAuthenticationChallenge:当然您可以将消息发送给您的代表,并包含一个质询,但这根本不是您想要的:这就是 挑战委托(delegate),而不是服务器通过您的 UIWebView 来挑战。

关于objective-c - didReceiveAuthenticationChallenge 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626881/

相关文章:

ios - 从图库中裁剪图像

iphone - 如何让2个UILabels的文本匹配?

iphone - 从 UIPageViewController 中移除 View Controller

iphone - 通过触摸移动事件限制拖动距离

ios - 在 arc 中调整 UIImage 大小时发生内存泄漏

iphone - UITableView 无限滚动

ios - 如果 iOS 变得无响应,iOS 会终止我的应用程序吗?

ios - 测试自动续订订阅

iphone - 在 Xcode 中调试时如何打印整数值?

iphone - 当应用程序已经上传时,ios 更改标签 itunesconnect