Created
October 17, 2020 14:52
-
-
Save mchirico/03ffa58b566fb6a186d3ac45a53140d2 to your computer and use it in GitHub Desktop.
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 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