Skip to content

Instantly share code, notes, and snippets.

@rayfix
Created June 25, 2016 17:20
Show Gist options
  • Select an option

  • Save rayfix/d9d77bdce7a9b1301b827194393ac89e to your computer and use it in GitHub Desktop.

Select an option

Save rayfix/d9d77bdce7a9b1301b827194393ac89e to your computer and use it in GitHub Desktop.
A simple network request using NSURLSession
import UIKit
import XCPlayground
var str = "Hello, playground"
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["foo": "bar"]
let session = NSURLSession(configuration: config)
let url = NSURL(string: "http://www.google.com/")!
let task = session.dataTaskWithURL(url) { data, response, error in
if let data = data {
let string = NSString(data: data, encoding: NSASCIIStringEncoding)
print("Hello \(data) \(string)")
}
}
task.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment