NSURLSessionDataDelegate method didReceiveData and others are not called

安稳与你 提交于 2019-12-21 11:22:58

问题


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 !


回答1:


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
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/27828593/nsurlsessiondatadelegate-method-didreceivedata-and-others-are-not-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!