【发布时间】:2012-04-03 04:25:24
【问题描述】:
我正在使用新的 iOS 5 方法发出异步 url 请求:sendAsynchronousRequest:queue:completionHandler:。这使用块来处理响应,但没有调用 NSURLConnectionDelegate 委托方法?在这种情况下,我看不到为 NSUrlConnection 类设置委托的方法吗?
[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: 委托方法 - 但在这种情况下没有调用委托。
谢谢!
【问题讨论】:
-
对委托使用其中一种连接方法,例如 initWithRequest:delegate:
标签: ios cocoa-touch