Last active
September 9, 2015 20:41
-
-
Save cezarcp/aa5287b7752c757541aa to your computer and use it in GitHub Desktop.
Making HTTP Requests in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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() |
Works for me
does not work
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()
}
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
does not work ....