Created
July 7, 2017 15:37
-
-
Save LuisMDeveloper/4d81f5da11ecea2b967269dd962053d4 to your computer and use it in GitHub Desktop.
URLSession example
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
let privatekey = "" | |
let apikey = "" | |
let ts = Date().timeIntervalSince1970.description | |
let hash = "\(ts)\(privatekey)\(apikey)".md5() | |
//var baseURL = "https://gateway.marvel.com:443" | |
let url = URL(string: "https://gateway.marvel.com/v1/public/comics?ts=\(ts)&apikey=\(apikey)&hash=\(hash)") | |
let defaultSession = URLSession(configuration: .default) | |
let dataTask: URLSessionDataTask? | |
dataTask = defaultSession.dataTask(with: url!, completionHandler: { (data, resp, error) in | |
if error != nil { | |
print(error!.localizedDescription) | |
} else { | |
if let data = data, let response = resp as? HTTPURLResponse, response.statusCode == 200 { | |
print(data) | |
do { | |
var dict = try JSONSerialization.jsonObject(with: data, options: []) | |
print(dict) | |
} catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} | |
} | |
}) | |
dataTask?.resume() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment