Last active
November 28, 2023 07:39
-
-
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
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
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