Search code examples
iosobjective-cafnetworking-2

Handling GET/POST/ PUT/ DELETE upcoming Requests in Base Class all together in Afnetworking


I have made a connection manager base class, which handles POST requests. But i have some request in GET and PUT.

How i can to handle GET/POST/PUT through connection manager class in below Code.

Let me know i am doing in right way to make request using void -

My Connection Manager Class, which handles only POST request -

.h

typedef NS_ENUM(NSUInteger, HttpRequestMethod)
{
 HttpRequestMethodPOST,
 HttpRequestMethodGET
};

@interface VCConnectionManager : NSObject
+ (instancetype) sharedInstance;
- (void) actionmethod:(NSString *)action     parameters:(NSDictionary *)params  onComplete:(void (^)(NSMutableDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock;

@end

.m

@implementation VCConnectionManager

+(instancetype) sharedInstance {
 DEFINE_SINGLETON_WITH_BLOCK(^{
 return [[VCConnectionManager alloc] init];
});
}

 - (void) actionmethod:(NSString *)action parameters:(NSDictionary *)param  onComplete:(void (^)(NSMutableDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock {

 BOOL network = [self currentNetworkStatus];
 if(network){

        NSString     *weburl = WS_BASE_URL;
        NSString * completeRequestUrl = [NSString stringWithFormat:@"%@%@", weburl,action];
        DDLogVerbose(@"Complete_URL--->%@",completeRequestUrl);
        AFHTTPRequestOperationManager *manager =  [AFHTTPRequestOperationManager manager];
        AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];

        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        manager.requestSerializer = requestSerializer;
        [params setValue:@"0" forKey:@"loginType"];

       [manager POST:completeRequestUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject){
      successBlock(responseObject);
       DDLogVerbose(@"\n\n\n\nResponse Result---->%@",responseObject);
  }
  failure: ^(AFHTTPRequestOperation *operation, NSError *error){
      //DDLogVerbose(@"Error: %@", error);
      errorBlock(error);
  }];
 }
 else{
    UIAlertView *internetAlert = [[UIAlertView alloc]initWithTitle:AppName message:NETWORK_ERROR delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [internetAlert show];
 }
 }

Solution

  • Add a method parameter and call it something like methodType. For brevity, I'm not typedef'ing this, but you should.

    - (void)actionmethod:(NSString *)action methodType:(NSString*)methodType parameters:(NSDictionary *)param onComplete:(void (^)(NSMutableDictionary *json))successBlock onError:(void (^)(NSError *error))errorBlock { // etc.
        ...
        if ([methodType isEqualToString:@"POST"]) {
            [manager POST:completeRequestUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject)...
        } else if (([methodType isEqualToString:@"GET"]) {
            [manager GET:completeRequestUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject)...
        }
        // etc.