var session = NSURLSession.sharedSession()
session.dataTaskWithRequest(urlRequest,
completionHandler: {(data: NSData!,
response: NSURLResponse!,
error: NSError!) in
println(data)
println(response)
println(error)
})
So I am making this request, and the completion block is never called.
What's wrong?
Also I tried a synchronous and asynchronous form
of the same request with NSURLConnection
and it worked perfectly.
EDIT:
I tried assigning a dataTask
variable to the session.dataTaskWithRequest
and displayed it right after. It says this <__NSCFLocalDataTask: 0xb41bda0> { suspended }
Suspended? Why?
So I tried calling it like this
And it worked.
Seems like I have to call
resume()
on a default suspended session task.This is a fairly unique case, but if you're using a URLSession inside a unit test, you'll need to setup the expectations. Otherwise, your test case will end and it will appear that your request is never returning. Swift 3.0.1.
Are you using playgrounds??
If you are, you should be careful to include:
In order to make the playground wait for the callback
You can also use it simply by :-
I face the same problem and I solved it by
And check that you are added the
App Transport Security Settings
in yourinfo.plist
.It'll be something like this in Swift 2.x