xcode NSURLConnection sendAsynchronousRequest:queue:completionHandler: 不调用委托方法 - didReceiveAuthenticationChallenge

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10207596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:00:24  来源:igfitidea点击:

NSURLConnection sendAsynchronousRequest:queue:completionHandler: doesn't call delegate method - didReceiveAuthenticationChallenge

xcoderestdelegatesnsurlconnectiondigest

提问by milepile

I have a REST API which is secured by digest. I want to download my JSON response, but first I've to authenticate against the rest api. I'm doing my Requests with sendAsynchronousRequest:queue:completionHandler:. But I don't know how to handle the digest authentication. I thought with the delegate method didReceiveAuthenticationChallenge of NSURLConnectionDelegate this should be possible? I've declared in the .h file the NSURLConnectionDelegate and added in the implementation the method. But nothing happens. Any advice how to handle this with "sendAsynchronousRequest:queue:completionHandler:" ?

我有一个受摘要保护的 REST API。我想下载我的 JSON 响应,但首先我必须针对其余 api 进行身份验证。我正在用 sendAsynchronousRequest:queue:completionHandler: 做我的请求。但我不知道如何处理摘要认证。我想用 NSURLConnectionDelegate 的委托方法 didReceiveAuthenticationChallenge 这应该是可能的吗?我已经在 .h 文件中声明了 NSURLConnectionDelegate 并在实现中添加了该方法。但什么也没有发生。任何建议如何使用 "sendAsynchronousRequest:queue:completionHandler:" 处理这个问题?

NSURL *url = [NSURL URLWithString:@"http://restapi/"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if ([data length] > 0 && error == nil)
         [self receivedData:data];
     else
         NSLog(@"error");
 }];

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"did get auth challenge"); }

回答by Codo

The connection:didReceiveAuthenticationChallenge:will only be called if you specify your instance as the delegateof the connection. To do so you'll need to use a different method to start the request, e.g.:

connection:didReceiveAuthenticationChallenge:如果您指定实例作为才会被调用委托的连接。为此,您需要使用不同的方法来启动请求,例如:

NSURL *url = [NSURL URLWithString:@"http://restapi/"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]

You will need to implement further delegate methods in order to receive the response.

您将需要实现更多的委托方法才能接收响应。

Note that connection:didReceiveAuthenticationChallenge:is deprecated in favor of other delegate methods (see this page).

请注意,connection:didReceiveAuthenticationChallenge:不推荐使用其他委托方法(请参阅此页面)。

回答by Ravi Dalmia