Network requests is something that basically every app needs and can be made many different ways. In this article you'll learn how to make these requests without adding extra unneeded libraries.
the Quest for a simple network manager
Many, if not most, of the apps I’ve worked on require some sort of network manager and all those that do will connect to one and the same api; often a proxy between the app and several other backend services; either being it in-house at a client or for one of my personal apps.
There are many different ways of introducing network support in your app; there are third party libraries that you can use which offer loads of different options (one of those that come to mind is Alamofire) or you can make one yourself. It is easier than you think.
The network manager
In many cases the most common network requests are GET and POST requests; on top of that, some apps fetch images asynchronous as well. In this article we are going to focus on the former two. These requests don’t require a full blown network library (which by the way will add to your apps binary size and also increase the startup time of your app). So what should you use instead? Well, the answer is simple, URL Loading System. This is what most, if not all, of those 3rd party libraries use underneath.
If you are used to using e.g. Alamofire, using URLSession and URLRequests might frighten you a little but there is no reason for fear. To make a simple GET requests, all you need is a few lines of code. For the example below I’ve setup a mockAPI at https://mockapi.io so you can tryout the code directly in a playground.
Most of the code above might seem like a lot, but it really is not, it is the documentation I’ve added that makes it look like there is a lot. So what does it do?
The code above will try to make a GET request towards the url https://60c86ffcafc88600179f70e2.mockapi.io/api/getRequest. If it succeeds it will return a Result with type [User], and if it fails it will return a Result with type Error. Yes, I added Result with capital R here.. this is because I am referencing a type of object that is returned, one that is really handy to use in Swift.
So what if you want to make a POST request? Just add the method parameter to the request type.
What if I just want Data?
This is easy too, you don’t need to create yet another request method… it works with the one you’ve already added. There are only 3 lines of code you need to add just after the guard let data = data part and before the Do / Catch block; those lines are the following:
and then you can call your request like so:
So what did we do? Well, out of the box, Data already conforms to Decodable; however this requires the content of Data to be in a JSON format. However, since Data already conforms to Codable we do not have to function overload the request method with a different type.. we can just use the one with the T: Decodable generic type instead. However, before we decode we need to check if the result data we get back conforms to the type we actually are looking for (In this case Data), and since we’re just looking for Data and data will conform to Data, we can just return that object directly.
This is how simple it is to make GET or POST requests, without pulling in a whole network library. Keep it simple, and use the power of Swift with Generics, Result and URL Loading System.
In this post you will learn how to add the calendar page to your app. For the SwiftIsland app, I created a schedule view for the activities during a workshop day; this schedule view has now been seperated to its own SPM called SimpleCalendar. In this tutorial you will learn how to add this schedule page to your app.