Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Last active November 28, 2023 07:39
Show Gist options
  • Save jayesh15111988/fd80a85b72fadada398d74cef72b97f7 to your computer and use it in GitHub Desktop.
Save jayesh15111988/fd80a85b72fadada398d74cef72b97f7 to your computer and use it in GitHub Desktop.
A code for demonstrating how to use URLSession to send network request from iOS app
struct Name: Decodable {
let firstName: String
let lastName: String
}
final class NetworkService {
func loadSampleData(completion: @escaping (Result<Name, Error>) -> Void) {
let url = URL(string: "www.google.com")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
let decoder = JSONDecoder()
let responsePayload = try! decoder.decode(Name.self, from: data!)
completion(.success(responsePayload))
}
task.resume()
}
}
//How to use API
NetworkService().loadSampleData { result in
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment