Skip to content

Instantly share code, notes, and snippets.

@mchirico
Created October 17, 2020 14:52
Show Gist options
  • Save mchirico/03ffa58b566fb6a186d3ac45a53140d2 to your computer and use it in GitHub Desktop.
Save mchirico/03ffa58b566fb6a186d3ac45a53140d2 to your computer and use it in GitHub Desktop.
import UIKit
struct Session {
var url: String
func Get(completion: @escaping (_ result: String) -> Void, onFailure: () -> Void) {
let configuraion = URLSessionConfiguration.default
let session = URLSession(configuration: configuraion)
guard let _url = URL(string: url) else { fatalError()}
let task = session.dataTask(with: _url) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
(200..<300).contains(httpResponse.statusCode) else {
return
}
guard let data = data else {
return
}
if let result = String(data: data, encoding:. utf8) {
print(result)
completion(result)
}
}
task.resume()
}
}
func mySession(url: String) {
let configuraion = URLSessionConfiguration.default
let session = URLSession(configuration: configuraion)
guard let _url = URL(string: url) else { fatalError()}
let task = session.dataTask(with: _url) { data, response, error in
guard let httpResponse = response as? HTTPURLResponse,
(200..<300).contains(httpResponse.statusCode) else {
return
}
guard let data = data else {
return
}
if let result = String(data: data, encoding:. utf8) {
print(result)
}
}
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment