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.