Skip to content

Instantly share code, notes, and snippets.

@yabenatti yabenatti/NetworkManager.h
Last active Jun 14, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.