Skip to content

Instantly share code, notes, and snippets.

@jaisontj
Last active October 14, 2015 03:47
Show Gist options
  • Save jaisontj/0eedc89fc5c44c7c14a1 to your computer and use it in GitHub Desktop.
Save jaisontj/0eedc89fc5c44c7c14a1 to your computer and use it in GitHub Desktop.
HTTPGet from Google with mods for swift 2.0
import Foundation
func HTTPsendRequest(request: NSMutableURLRequest,callback: (String, String?) -> Void) {
let task = NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler :
{
data, response, error in
if error != nil {
callback("", (error!.localizedDescription) as String)
} else {
callback(NSString(data: data!, encoding: NSUTF8StringEncoding) as! String,nil)
}
})
task!.resume() //Tasks are called with .resume()
}
func HTTPGet(url: String, callback: (String, String?) -> Void) {
let request = NSMutableURLRequest(URL: NSURL(string: url)!) //To get the URL of the receiver , var URL: NSURL? is used
HTTPsendRequest(request, callback: callback)
}
HTTPGet("http://www.google.com") {
(data: String, error: String?) -> Void in
if error != nil {
print(error)
} else {
print("data is : \n\n\n")
print(data)
}
}
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment