-
-
Save hsavit1/a8582a92eefdeda6c91e to your computer and use it in GitHub Desktop.
HTTPGet from Google with mods for swift 2.0
This file contains 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
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