NSURLSessionDataTask dataTaskWithURL completion handler not getting called

If you’re going to use the completion block rendition of the data task, rather than specifying a delegate of nil, like this:

NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
                                                             delegate: nil
                                                        delegateQueue: [NSOperationQueue mainQueue]];

You should instead instantiate your session using the method that does not take a delegate at all:

NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject];

Or, alternatively, given that you’re not customizing your configuration at all, you can skip the instantiation of the NSURLSessionConfiguration altogether and use the shared NSURLSession:

NSURLSession *defaultSession = [NSURLSession sharedSession];

But, bottom line, you should not use the sessionWithConfiguration:delegate:queue: rendition unless you’re going to implement the delegates, in which case you wouldn’t use the rendition of the NSURLSessionDataTask method with the completionHandler parameter.

Also, make sure your device/simulator is running iOS 7.0 or greater.

Leave a Comment