1
votes

I'm trying to update an old application to move from NSURLConnection to NSURLSession. Everything works fine when I use blocks for NSURLSession:

 NSURLSessionDataTask *dataTask = [session
                                          dataTaskWithRequest:request 
 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}

But sometimes I need to analyse the data that the app is receiving from server, so I need didReceiveResponse and didReceiveData methods, similar to the ones used with NSURLConnection. I'm trying to do it like this:

 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

 NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
                                                      delegate:self
                                                 delegateQueue:nil];
 NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.apple.com"]];
 [dataTask resume];

But my delegates are never called:

- (void)URLSession:(NSURLSession *)session
 NSURLSessionTaskDelegatedidReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
    NSLog(@"-----------------------> NSURLSessionTaskDelegatedidReceiveResponse");
    completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    NSLog(@"----------------> didReceiveData");
}

Same thing happens when I'm trying to use other types of tasks (NSURLSessionDownloadTask for example), their delegates are never called. I can actually see in the debugger that the tasks are switching to running state, but I can never get any calls to delegates. In my header file I do conform my ViewController to delegates:

@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate>

I did quite a bit of searching but even when I'm trying some working examples from the web they just stop working as soon as I try to paste them in my project. For example this tutorial works perfectly for me (I do have to add Transport Security permissions to plist file, but that's about it). But when I'm trying to use this code in my project delegates are never called. And when I'm actually paste my code to another simple application it works just fine. So something probably wrong with my project's settings? Could you please show me what can cause something like this? I'm completely bewildered at the moment.

1
Unfortunately my problem is not related to URL. I tried to use a few of a different ones, and initially I was using the URL that was used before with my NSURLConnection via [session dataTaskWithRequest:request]; (it was of course with https:// ).Evgeny

1 Answers

3
votes

You are using completion blocks. That exclude delegate calls. https://developer.apple.com/reference/foundation/nsurlsessiondatadelegate?language=objc

Completion handler block are primarily intended as an alternative to using a custom delegate. If you create a task using a method that takes a completion handler block, the delegate methods for response and data delivery are not called.

And the method you want to use has a different name to

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler