I've been searching around to learn how to get some data using NSURLSession in Swift. There is a general pattern being repeated on the internet for making an http request using NSURLSession, and it looks a lot like this:
let url = NSURL(string: "myPretendURLthatIsNotThisURL.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding))}
task.resume()
If I place a real URL in, I can see a lot of JSON output printed, which is the expected response from the server. Nice : ) I can see the data from println(). However, I would rather display the returned data in a text view when I press a button. I do believe my solution relies on using a delegate (somehow). Is it all right to request an example of how this is done, in the following context:
- Code is in Swift
- Press a button (IBAction, contains the code above to make the request)
- Display the data returned (it's JSON, I'll worry how to parse it later on) in a text view.
Thanks!