Calling Your API Through Singleton Class

Neha Sharma
2 min readSep 28, 2017

Hello Everyone,

Today i will explain about Singleton Class & how you can hit your api and get response through Singleton Class.

Singleton Class is a special class which have only one instance. It has global variables and global functions.

We can use Singleton Class to hit our API’s and get response without writing the same code again and again for difference API’s.

So, Let’s start with creating Singleton Class.

  1. Right Click on File –> Select New File –> Cocoa Touch –> SubClass NSObject

2. Create your own Protocol & Delegate Methods for GET & POST(With parameters or without parameters) API’s (You can make your won methods according to your requirements)

  • -(void)postOrGetData:(NSString *)UrlString postPar:(id )postParaDict method:(NSString *)methodType isForrmData:(BOOL)formData setHeader:(BOOL)header successHandler:(void(^)(id response))successHandler failureHandler:(void(^)(id response))failureHandler imageDownloader:(BOOL)imageDownloader;
  • -(void)postOrGetFormData:(NSString *)UrlString strParameters:(NSString *)strParam postPar:(id )postParaDict method:(NSString *)methodType setHeader:(BOOL)header successHandler:(void(^)(id response))successHandler failureHandler:(void(^)(id response))failureHandler;

3. Now Import Singleton Class & baseURL in your ViewController add call delegate methods of Singleton Class.

Create instant of singleton Class and call its methods and pass paramters.

4. After getting response, save users details in NSUserDefault for future reference.

Thanks for reading this article. I hope it enriched your existing knowledge. Please share your reviews.

Follow Me on: Facebook | Twitter | GitHub | LinkedIn

--

--

Neha Sharma

Sr. iOS Developer & Free Time Blogger. I am both driven and self-motivated and constantly experimenting with new technologies and techniques.