[iOS] – Create API wrapper with AFNetworking ?>

[iOS] – Create API wrapper with AFNetworking

Okay, so why AFNetworking? Well, this is a small quote from github:

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.

That’s enough for a library right? Let’s start!

1. Installation

There are some way to install the library, I choose the Pods method because it’s simple and easy to upgrade after.

Open the podfile then use this line to install AFNetworking:

Run the pods install command and everything be done.

2. Create the API

Next, we will create out API wrapper. To make it simple, this is just a API which calling to server with the user_id then server will return the user profile.

APIClient.h

APIClient.m

Some init methods:

API method:

Too clearly in the code, so nothing need to be explain here :trollface:

Support method:

As you can see, in the getAFHTTPRequestOperationManager I can set the authToken to header for the authentication from server, you can add yours which anything you need to verify with your server.

 

Okayyyyyy, let’s see how to use this API wrapper:

AnotherViewController.m

Wohooooooo too simple right?

Refs: https://github.com/AFNetworking/AFNetworking

Have fun!

3 thoughts on “[iOS] – Create API wrapper with AFNetworking

Leave a Reply

Your email address will not be published. Required fields are marked *