Almost every iOS app is backed-up by some back-end. That is very common architecture. Our iOS app communicates with back-end, grabs data from its web services and presents them to the end user. Facebook, Twitter, Instagram, Pinterest, for example, also have that same architecture.
RESTful web services basically became a default for many web and iOS apps. REST stands for Representational State Transfer and it is primarily used to build Web services that are lightweight, maintainable, and scalable. A service based on REST is called a RESTful service. REST is not dependent on any protocol, but almost every RESTful service uses HTTP as its underlying protocol.
When we ask our web service to give us something, it can respond to us in several ways but the most common response is in JSONformat.
So, let’s try and build some RESTManagerfor our projects which we can use in every single app that we’re making.
Every single HTTP request has a HTTP method. Four of them are widely used and they are GET, POST, PUT and DELETE. They are used for CRUD operations (Create, Read, Update, Delete) on our back-end database.
When we send our HTTP request, server responds to us with a HTTP Response. One of the parameters in that response is statusCode, which tells us whether the request is ok, is the server working, does the server has that page that we’re asking for, etc.
JSON data type is the same as our Swift Dictionary, so we should make our life a little easier.
By using typealias, we basically say to Swift – whenever you find the word JSON – replace it with [String: Any]. Why do we need the other one: [JSON]? Most of the times, we get a response as an array of dictionaries, i.e. an array of JSONs. We want to cover that case too.
Now, we can make our class RESTManager. We will make it as Singleton, so that every time when someone asks our RESTManager, he will always get the same instance.
Now, every single HTTP request has an url, a HTTP method and a list of parameters, in case we need something to send to the server.
As you can see, we want JSON to be our content type. Besides that, if we want to send some parameters to our web server – we will put them as httpBody of our URLRequest.
Finally, we will work on our public function which we will use in order to get our JSON response from our server.
In a nutshell, we create an URLSession and tell it to perform a data request which we previously create via our private function. When we get a response – we check to see if we have an error, and if yes – we print it out. Of course, we do that in the main thread, because if we want to present that error to our end user, UI-related stuff should all be handled inside the main thread.
if there is no error and we grab a bag of bits (Data) from our server, we try to parse it with JSONSerialization object, to make sure that we get a JSON and not XML for example. After we successfully parse it, we send it to our closure along with our status and of course, we do that in main thread too.
Now when we want to use our RESTManager in our code, all we have to do is:
Finally, we have our response:
Of course, your response will differ from mine, but I think you get the idea 😉
Of course, GitHub page for the final project is available here.