Skip to content

Instantly share code, notes, and snippets.

@mikekavouras
Last active April 28, 2016 17:42
Show Gist options
  • Save mikekavouras/e41ec5e75c0346bf9a1144ecf0f81bf4 to your computer and use it in GitHub Desktop.
Save mikekavouras/e41ec5e75c0346bf9a1144ecf0f81bf4 to your computer and use it in GitHub Desktop.
func getRequest(urlString: String, completion: (NSDictionary) -> Void) {
let defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let urlPath: String = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let url: NSURL = NSURL(string: urlPath)!
let request: NSURLRequest = NSURLRequest(URL: url)
do {
let task = defaultSession.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
do {
if data != nil {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
completion(jsonResult)
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
})
task.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment