Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSessionDataDelegate method didReceiveData and others are not called

Tags:

swift

ios8.1

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="+"[email protected]&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 !

like image 667
Ria Bhattacharjee Avatar asked Jan 07 '15 21:01

Ria Bhattacharjee


1 Answers

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
}
like image 98
ChezhianNeo Avatar answered Sep 24 '22 13:09

ChezhianNeo