7

I am having a problem that didReceiveData and didCompleteWithError are not called. Here is my code :

class LoginViewController: UIViewController, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate {
.
.
.
}

@IBAction func loginAction(sender: AnyObject) {

    var sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()

    var session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue:nil)

    let postParams = "email="+"rabcd@gmail.com&password="+"abcd"

    let url = NSURL(string:"http://myurl.com/api/v1/user/login")
    let request = NSMutableURLRequest(URL: url!)

    request.HTTPMethod = "POST"

    request.HTTPBody = postParams.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)

    let task = session.dataTaskWithRequest(request)


    task.resume()


}

These are delegates I implemented

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {

}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {

}

func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {

}

Here I watched with break points

didReceiveResponse is called but other two are not getting called.

Please help !

mkobit
  • 43,979
  • 12
  • 156
  • 150

3 Answers3

15

Implement the completion handler in your delegate method

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
completionHandler(NSURLSessionResponseDisposition.Allow) //.Cancel,If you want to stop the download
}
ChezhianNeo
  • 365
  • 1
  • 12
  • Completion handler works good that I already know but I wanted to go with the delegate method. – Ria Bhattacharjee Mar 05 '15 at 19:59
  • 2
    To make the other delegate methods work you should call the completion handler with NSURLSessionResponseDisposition.Allow – ChezhianNeo Mar 06 '15 at 11:54
  • definitely will try it :) – Ria Bhattacharjee Mar 27 '15 at 12:13
  • Expanding on this answer by providing a link to the relevant part of Apple's docs - https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSessionDataDelegate_protocol/#//apple_ref/occ/intfm/NSURLSessionDataDelegate/URLSession:dataTask:didReceiveResponse:completionHandler: – D6mi Jun 06 '16 at 07:50
4

I'll confirm ChezhianNeo's comment about calling the didRecieveResponse delegate's completionHandler with NSURLSessionResponseAllow, as shown below

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

completionHandler(NSURLSessionResponseAllow);
}

This enabled the didRecieveData delegate method to be called as well.

What also works, at least it did for me, is to simply not implement the didReceiveResponse method in your delegate, but do implement the didReceiveData method - "skipping" the didReceiveResponse method allows didReceiveData method to be called, which doesn't seem to make a whole lot of sense, but it works.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
pmchallam
  • 41
  • 1
2

For others :
In my case, the delegate didReceiveData was not called even after I :
1. set the delegate in NSURLSession.
2. implement didReceiveResponse with NSURLSessionResponseAllow

The reason :
Using dataTaskWithRequest with completion handler prevent calling the delegate.
you must use the dataTaskWithRequest without completion to use NSURLSession delegates.

user1105951
  • 2,259
  • 2
  • 34
  • 55