Skip to content

Instantly share code, notes, and snippets.

@cezarcp
Last active September 9, 2015 20:41
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cezarcp/aa5287b7752c757541aa to your computer and use it in GitHub Desktop.
Making HTTP Requests in Swift
//Uncomment both lines below if testing on an Xcode playground.
//import XCPlayground
//XCPSetExecutionShouldContinueIndefinitely()
let url = NSURL(string: "http://www.stackoverflow.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
@lteu
Copy link

lteu commented Nov 11, 2014

does not work ....

@werne2j
Copy link

werne2j commented Nov 11, 2014

Works for me

@hcri50
Copy link

hcri50 commented Nov 30, 2014

does not work

@GLLefavi
Copy link

GLLefavi commented Apr 9, 2015

You need to put it in a function to work, such as:

override func viewDidLoad() {
super.viewDidLoad()
test()
}

func test() {
    println("Start Test")
    let url = NSURL(string: "http://www.stackoverflow.com")

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    }

    task.resume()
}

@fortmarek
Copy link

This works for me but for whatever reason, it doesn't work for this site: http://old.gjk.cz/suplovani.php
Any idea why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment