2012-04-03 46 views
5

我使用新的iOS 5方法发出异步url请求:sendAsynchronousRequest:queue:completionHandler :.这使用块来处理响应,但是没有调用NSURLConnectionDelegate委托方法?我看不到在此实例中设置NSUrlConnection类的委托的方法?使用sendAsynchronousRequest处理401响应:queue:completionHandler:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

    NSLog(@"HTTP response code: %i", httpResponse.statusCode); 

    if (error != nil) { 
     NSLog(@"There was error with the synchronous request: %@", error.description);    
    } 
}]; 

我之所以需要委托方法是因为我的电话的人得到一个401响应,iOS系统处理以不同的方式 - 推迟认证委托方法。当401响应返回时 - httpResponse.statusCode只是“0”。

有没有办法阻止iOS试图处理401不同?特别是,我想我需要使用continueWithoutCredentialForAuthenticationChallenge:委托方法 - 但在这种情况下没有委托被调用。

谢谢!

+0

使用与代表,像initWithRequest的连接方式之一:代表: – 2012-04-03 04:30:01

+0

看一看这个答案: http://stackoverflow.com/questions/12828060/authentication-with-nsurlconnection-sendasynchronousrequest-with-完成手 – 2013-07-29 12:34:23

回答

1

您需要使用NSURLConnection实例并为其使用委托。 sendAsynchronousRequest:queue:completionHandler:是一个相同的快捷方式,但具有没有委托的默认行为。

0
NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

NSURLConnection *connection = [[NSURLConnection alloc] nitWithRequest:request delegate:self]; 

使用上面的方法会触发委托方法。