2016-06-07

Manage HTTP Requests Through APIs In Your iOS App

Note: There is a rating embedded within this post, please visit this post to rate it.

Pham Van Hoang, hoangk55cd@gmail.com, is the author of this article and he contributes to RobustTechHouse Blog

Most of applications today need to communicate with servers to exchange data. When an app needs to send data to the server, or fetch data from it, it makes the proper request for the purpose. That happens many times during the app runtime period. As an iOS developer will definitely run into it sooner or later. In this tutorial, I will introduce you:

AFNetworking: an iOS “must-have” library to deal with networking in iOS app.

How to design a APIManager class that easy to manage your HTTP requests (APIs) and reuse.

Let’s get started!

AFNetworking

“AFNetworking is a delightful networking library for iOS and Mac OS X. It’s built on top of the Foundation URL Loading System, extending the powerful high-level networking abstractions built into Cocoa. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use.

Perhaps the most important feature of all, however, is the amazing community of developers who use and contribute to AFNetworking every day. AFNetworking powers some of the most popular and critically-acclaimed apps on the iPhone, iPad, and Mac” – AFNetworking page.

To use AFNetworking is pretty easy. For every request, you will create an AFHTTPRequestOperationManager or AFHTTPRequestOperation instance and send a GET/POST/PUT/DELETE request to handle the response. For example.

It’s a simple and powerful library, huh!

However, you definitely will not want to rewrite the get/post/put/delete methods and the functions to check and handle errors in every request, since it will take time and be potentially hard to manage. Also whenever possible, try to avoid duplicating code.

Manage HTTP Requests (APIs)

To do that, we will need: a class that handle all requests; a class check to handle all errors; and a base class that handle common response data from server. In this example I created 3 classes: APIBaseObject, APIManager, APIErrorProcessor.



APIBaseObject

This is a base class that parse common response from server, every model object return from server will inherit from this class. You can see that in this class, we can handle the common response. For example: every response from server may have message key, and status key. You will handle in this base class instead of handling in every model object.

APIManager

This is the class that handle and manage all requests.

In this .h file, we will have

requestOperationManager: an object that stores all requests and set maximum concurrent requests at time for our project.

A cancel current request function.

A custom GET/POST/PUT/DELETE request, we will make a request and handle all errors and data inside this function. It will return the response object as a result (in this example, I just demonstrate the POST request).

Now, head over to .m file to implement the code that actually create and handle the request.

Inside the request function, it calls APIErrorProcessor to handle the errors. Let’s take a look inside. This class will check the common network error and also the errors defined by developers.

Pretty awesome, huh? Now you can create your API Manager for your own project, I will add the full code on my Github repository here.

Thanks for reading, if you have any questions, please leave your comments below. I will post my demonstration application using APIManager any time soon.

Brought to you by the RobustTechHouse team. RobustTechHouse works on mobile app development projects and eCommerce & web development projects. If you like our articles, please also check out our Facebook page.

The post Manage HTTP Requests Through APIs In Your iOS App appeared first on RobustTechHouse.

Show more