Skip to content

Instantly share code, notes, and snippets.

@BlaShadow
Created May 13, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlaShadow/6ba86df3656e7b0150020489da8e0054 to your computer and use it in GitHub Desktop.
Save BlaShadow/6ba86df3656e7b0150020489da8e0054 to your computer and use it in GitHub Desktop.
Swift HTTP Call
httpCall(url: 'http://worldtimeapi.org/api/timezone/Europe/Rome') { (result) in
print(result)
}
func httpCall(url: String, completion: @escaping (_: [String: Any]) -> Void) {
let url = URL(string: url)!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
completion(json ?? [:])
} catch {
print("JSON error: \(error.localizedDescription)")
completion([:])
}
}
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment