A Generic iOS network handler leveraging URLSession
Looking for a reliable and efficient solution for network communication in your iOS applications? Look no further than our iOS network handler. Our powerful tool leverages the capabilities of URLSession to provide a flexible and easy-to-use interface for managing and executing network requests within your app.
- Generic design suitable for a wide range of use cases
- Built specifically for iOS for optimal performance and reliability
- Integrates seamlessly with URLSession framework
- Streamlines your app's network communication for a seamless user experience
To get started with our network handler, simply install the package and import it into your project. You can then begin using the tool to manage and execute network requests within your app.
♦ Open your Xcode project.
♦ Go to File > Swift Packages > Add Package Dependency. This will open a window to add a new package.
♦ In the "Add Package Dependency" window, you can enter the URL https://github.com/Sidney-Okine/GenericNetworker.git
♦ After you've entered the package information, click "Next".
♦ In the next screen, you can select the version of the package you want to use. You can also specify a branch, tag, or commit to use.
♦ Select the option "Up to the next major version" to keep your package up to date.
♦ In the next screen, you can choose the targets for the package. You can select one or more targets to add the package to.
♦ After you've selected the targets, click "Finish". Xcode will then download and add the package to your project.
♦ After you've added the package to your project, you can import the package in your Swift code using the import statement. For example, import my package like so:
import GenericNetworker
Here is a sample use case for my network handler:
A simple example of a Network Call to the Jokes API.
-
- First define your Codable structs, for both response and request ( but in our case we'll only require a response struct )
// Response struct
// For the purposes of this demo, I have defined the entire structure. 👍🏾
struct JokesModel : Codable {
let error : Bool?
let category : String?
let type : String?
let joke : String?
let flags : JokeFlags
let id : Int?
let safe : Bool?
let lang : String
}
struct JokeFlags : Codable {
let nsfw : Bool
let religious : Bool
let political : Bool
let racist : Bool
let sexist : Bool
let explicit : Bool
}
-
- Making the actual call 😄
Requester.shared.makeRequest(to: "https://v2.jokeapi.dev/joke", urlEndPoint: "/Programming", httpMethod: .GET, headers: httpHeaders(), expecting: JokesModel.self){result,error in
if error == nil {
// I prefer only the joke in the response as I don't really know what I'll do with the rest 😅
// You might as well call the entire struct like so; print(result)
print(result?.joke as! String)
}
else {
print(error?.localizedDescription)
}
}
The code above is a function call to a method called makeRequest that is defined in an object called Requester. The makeRequest method takes several arguments:
- to (your baseUrl) : A string that specifies the URL to which the request is being made.
- urlEndPoint : Another string that specifies the endpoint of the URL.
- httpMethod: An enumeration type that specifies the HTTP method being used (e.g. GET, POST, PATCH etc.).
- customBody: An optional argument that specifies the HTTP request body (if any).
- headers: An optional dictionary that specifies HTTP headers to be included in the request(if there are changes to the basic three i.e Content-Type,Accept,Authorization(if any)).
- additionalHttpHeaders: Another optional dictionary that specifies additional HTTP headers to be included in the request.
- expecting: A type that is both Decodable and Encodable thus Codable.
- completion: A closure that takes two arguments, a Codable object and an Error object. This closure is called when the request is complete
- My response in the console
Two C strings walk into a bar.
The bartender asks "What can I get ya?"
The first string says "I'll have a gin and tonic."
The second string thinks for a minute, then says "I'll take a tequila sunriseJF()#$JF(#)$(@J#()$@#())!*FNIN!OBN134ufh1ui34hf9813f8h8384h981h3984h5F!##@"
The first string apologizes, "You'll have to excuse my friend, he's not null-terminated."
The joke wasn't what I was hoping for 🙄 but I guess it's funny so permit me to laugh 🤣
Feel free to hit me up 🤙
Made with ❤️ by Sidney Okine