In this tutorial you will learn about how to fetch data from server or make HTTP Network request using inbuilt Apple’s URLSession
, We will use a singleton shared
method. You may have a different type of HTTP method like GET POST PUT DELETE
. We can use all of them to fetch/update data from Server
Let’s jump to the code part
Let’s take the GET
method example, so we will be picking up repo JSON from Github URL Inline comments have been added for simplicity.
let urlString = "https://api.github.com/users/janeshsutharios/repos"
//MARK: Start a Network Task
func startNetworkTask(urlStr:String, params:[String:Any], resultHandler: @escaping (Result<Data?, Error>) -> Void) {
guard let urlObject = URL(string:urlStr) else {
print("issue in url object")
resultHandler(.failure(CustomError.urlError))
return
}
// 1: Creating data task
URLSession.shared.dataTask(with: URLRequest(url: urlObject)) { dataObject, responseObj, errorObject in
if let error = errorObject {
resultHandler(.failure(error))
} else {
resultHandler(.success(dataObject))
}
}.resume()
}
}
// MARK: Model for JSON Data -- For Converting Network Data object into swift readable data object
struct GithubEntity: Codable {
var id: Int?
var nodeID, name, fullName: String?
}
// MARK: A custom error enum
enum CustomError: Error {
case urlError
}
// MARK: use a URL & make api call
let urlString = "https://api.github.com/users/janeshsutharios/repos"
var jsonModel:[GithubEntity] = []
startNetworkTask(urlStr: urlString, params: [:]) { result in
print("recieved data from api-->", result)
switch result {
case .success(let dataObject):
do {
// 1: updating data to swift Model
let decoderObject = JSONDecoder()
jsonModel = try decoderObject.decode([GithubEntity].self, from: dataObject!)
print("recieved data from codable model -->", jsonModel)
}
catch {
print("error--->", error)
}
case .failure(let error):
print(error.localizedDescription)
}
}
Likewise you can use the same function for POST api, You just need to pass parameters as dictionary
Feel free to add suggestion in comments