When trying to create my own session object NSURLSession()
and request an url I get an unrecognized selector exception but when I use the shared session NSURLSession.sharedSession()
everything works fine. How come?
var url = NSURL(string: "http:/www.google.com") if url != nil { //throws unrecognized selector when dataTaskWithURL is called let session=NSURLSession() session.dataTaskWithURL(url!) //works let sharedSession=NSURLSession.sharedSession() sharedSession.dataTaskWithURL(url!) }
You have to init URLSession
with a configuration:
URLSession(configuration: .default)
or use shared session
URLSession.shared
In SWIFT 3.0 and up:
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in //Your code }).resume()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With