Nsurlsessiondatadelegate Method Didreceivedata and Others Are Not Called

Cannot get didReceiveData to be called when using NSURLSession

For delegate pattern with NSURLSession, I think you should create NSURLSession by something like this:

[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:self
delegateQueue:[NSOperationQueue mainQueue]];

But not:

[NSURLSession sharedSession];

Also I noticed your NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate go with UIViewController; however, your NSURLSession and delegate methods are implemented in FileUpload.m. Try to edit this line:

@interface FileUpload : NSObject <RCTBridgeModule>

to:

@interface FileUpload : NSObject <RCTBridgeModule, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>

Then create NSURLSessionDataTask without completion handler:

NSURLSessionDataTask *task = [session dataTaskWithRequest:req];

And see if there is any difference.

NSURLSessionDataDelegate not called

class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionTaskDelegate {
var session:NSURLSession!

override func viewDidLoad() {
super.viewDidLoad()

let requestUrl = "https://www.google.com"
let request = NSURLRequest(URL: NSURL(string: requestUrl)!)
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

self.session = NSURLSession(
configuration: configuration,
delegate: self,
delegateQueue: NSOperationQueue.mainQueue()
)

let task = session.dataTaskWithRequest(request)

task.resume()
}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("Did receive data!")
}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
print("Response!!")
}
}

NSURLSessionDataTask delegate methods are not called

Use the method

+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration
delegate:(id<NSURLSessionDelegate>)delegate
delegateQueue:(NSOperationQueue *)queue

to set the delegate properly.

Update:

And you have to call the completion handler of didReceiveResponse

completionHandler

A completion handler that your code calls to continue the transfer,
passing a constant to indicate whether the transfer should continue as
a data task or should become a download task.

• If you pass NSURLSessionResponseAllow, the task continues normally.

• If you pass NSURLSessionResponseCancel, the task is canceled.

• If you pass NSURLSessionResponseBecomeDownload as the disposition, your delegate’s URLSession:dataTask:didBecomeDownloadTask: method is called to provide you with the new download task that supersedes the current task.

For example

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

NSURLSesssionDelegate NSURLSessionDataDelegate functions not triggering with dataTask

After quite some research, I found the problem and thought, I will post an answer in case anybody has the same problem.

Take a look at this function in the code:

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler (NSURLSessionResponseDisposition) -> Void) {
self.startTime = NSDate()
self.endTime = self.startTime
}

A look in the corresponding Apple Doc gives the solution.
You have to call the completion handler like this to continue the transfer:

completionHandler(NSURLSessionResponseDisposition.Allow)


Related Topics



Leave a reply



Submit