Alex Alex - 1 year ago 477
iOS Question

NSURLSession dataTaskForRequest:completion: unrecognized selector sent to instance

When trying to create my own session object

and request an url I get an unrecognized selector exception but when I use the shared session
everything works fine. How come?

var url = NSURL(string: "http:/")
if url != nil {
//throws unrecognized selector when dataTaskWithURL is called
let session=NSURLSession()

let sharedSession=NSURLSession.sharedSession()

Answer Source

You have to init NSURLSession with configuration:

NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download