Making HTTP Requests in Swift

As I mentioned in a previous post, I've been meaning to write more about programming here. While my initial idea was to set up a separate feed, I'll just post as I normally do and think about it on another occasion.

I'll start with a series of posts on Swift, Apple's new programming language for iOS and OS X development. The first few posts will be based on some of my answers to questions on Stack Overflow. So, without further adue, let us discuss simple HTTP GET Requests in Swift.

The same options available in Objective-C can be used with Swift. I'll only cover those that don't involve using third party libraries such as AFNetworking.

Using NSURLSession

First, initialize an NSURL object and an NSURLSessionDataTask from NSURLSession. Then, run the task with resume().

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()

Pretty straightforward. Also, the block syntax in Swift is much more pleasant to read and write, specially when benefiting from trailing blocks, which is possible since the block parameter is the last argument to the NSURLSessionDataTask initializer.

Using NSURLConnection

First, initialize an NSURL and an NSURLRequest:

let url = NSURL(string: "http://www.stackoverflow.com")
let request = NSURLRequest(URL: url)

Then, you can load the request asynchronously with:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

Or you can initialize an NSURLConnection:

let connection = NSURLConnection(request: request, delegate:someObject, startImmediately: true)

Just make sure someObject implements the NSURLConnectionDataDelegate protocol, and then deal with the data received in the appropriate delegate methods.

For more detail, check the documentation for the NSURLConnectionDataDelegate protocol

Testing on an Xcode playground

In order to try this on an Xcode playground, add import XCPlayground to your playground, as well as the following call:

XCPSetExecutionShouldContinueIndefinitely()

This will allow you to use asynchronous code in playgrounds.

Final remarks

While I provided code that uses both NSURLSession and NSURLConnection, NSURLSession is the preferred solution. Even though NSURLConnection isn't officially deprecated, this has been stated by Apple engineers on several occasions during WWDC sessions.

Finally, here's a gist with the NSURLSession based implementation.

Adapted from my answer on Stack Overflow