Session.dataTaskWithURL completeHandler never called

I have the following code:

let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey let url = NSURL(string: urlPath) let session = NSURLSession.sharedSession() println(url!) let task = session.dataTaskWithURL(url!, completionHandler: {(data, reponse, error) in println("Task completed") // rest of the function... }) 

The Handler completion function is never called. I tried to call the url in my browser, it works fine. I tried with a different url, it still does not work. I checked that my ios simulator can connect to the internet, it does.

I do not know why the function is not called, and since I do not have any error that is difficult to debug.

+6
source share
1 answer

A task never completes because it never starts. You need to manually start the data task using the resume() method.

 let urlPath = apiURL + apiVersion + url + "?api_key=" + apiKey let url = NSURL(string: urlPath)! let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url) { data, response, error in print("Task completed") // rest of the function... } task.resume() 
+26
source

Source: https://habr.com/ru/post/981781/


All Articles