Skip to content

Instantly share code, notes, and snippets.

@yabenatti
Last active June 14, 2017 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yabenatti/77539743014d1dff44aded26bfaea8ba to your computer and use it in GitHub Desktop.
Save yabenatti/77539743014d1dff44aded26bfaea8ba to your computer and use it in GitHub Desktop.
NetworkManager for AFNetworking
#import <Foundation/Foundation.h>
@interface NetworkManager : NSObject
+(NetworkManager*) sharedInstance;
-(void)callAPIWithParameters:(NSDictionary *)parameters andUrl:(NSString *)url andMethodType:(NSString *)methodType andCompletion:(void(^) (BOOL success, id response, NSString *message, NSError *error)) completion;
@end
#import "NetworkManager.h"
#import "AFHTTPSessionManager.h"
#import "UserUtils.h"
@implementation NetworkManager
static NetworkManager *sharedInstance = nil;
static AFHTTPSessionManager *manager = nil;
/*!
* @discussion Setup a singleton of NetworkManager
* @return this instance will make all the HTTP calls.
*/
+ (NetworkManager*)sharedInstance {
if(sharedInstance == nil) {
sharedInstance = [[self alloc] init];
manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
}
return sharedInstance;
}
-(void)callAPIWithParameters:(NSDictionary *)parameters andUrl:(NSString *)url andMethodType:(NSString *)methodType andCompletion:(void(^) (BOOL success, id response, NSString *message, NSError *error)) completion {
//Checks if there's a token to add to header
if([UserUtils retrieveFromUserDefaultWithKey:API_TOKEN]) {
[manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@", [UserUtils retrieveFromUserDefaultWithKey:API_TOKEN]] forHTTPHeaderField:@"Authorization"];
}
//Chooses method type
if([methodType isEqualToString:@"GET"]) {
[manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
completion(YES, responseObject, nil, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSDictionary *errorDictionary = error.userInfo;
completion(NO, nil, [errorDictionary objectForKey:@"NSLocalizedDescription"], error);
}];
} else if([methodType isEqualToString:@"POST"]) {
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
completion(YES, responseObject, nil, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSDictionary *errorDictionary = error.userInfo;
completion(NO, nil, [errorDictionary objectForKey:@"NSLocalizedDescription"], error);
}];
} else if([methodType isEqualToString:@"PUT"]) {
[manager PUT:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
completion(YES, responseObject, nil, nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSDictionary *errorDictionary = error.userInfo;
completion(NO, nil, [errorDictionary objectForKey:@"NSLocalizedDescription"], error);
}];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment