Hit Api with AFNetworking

APIManagerNew.h

#import <Foundation/Foundation.h>

#import “MBProgressHUD.h”

#import “Utility_ObjectiveC.h”

#import “AFNetworking.h”

#import “ProductDetailAllDataModels.h”

@interface APIManagerNew : NSObject

@property AFHTTPSessionManager *manager;

+ (APIManagerNew *)sharedAPIManager;

– (void)hitAPIuserRegistrationParameters:(NSDictionary *)dictionaryParameters currentView:(UIView *)view completionHandlerSuccess:(void (^)(NSDictionary *dictionaryResponse))success completionHandlerFailure:(void (^)(NSError *error))failure;

@end

APIManagerNew.m

@implementation APIManagerNew

#define USE_PRODUCTION_URL 0

#if USE_PRODUCTION_URL

static NSString *const BASE_URL = @”http://cherishgold.com/cherishws/&#8221;;

#else

static NSString *const BASE_URL = @”http://shagunn.info/cherishws/&#8221;;

#endif

NSString *const USERREGISTRATION = @”webservices/userRegistration”;

+ (APIManagerNew *)sharedAPIManager{

    static APIManagerNew *sharedInstance = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        sharedInstance = [[APIManagerNew alloc]init];

    });

    return sharedInstance;

    //    static NetworkManager *sharedNetworkManager = nil;

    //    static dispatch_once_t oncePredicate;

    //    dispatch_once(&oncePredicate, ^{

    //

    //        sharedNetworkManager = [[self alloc]initWithBaseURL:[NSURL URLWithString:SERVER_URL]];

    //        [sharedNetworkManager setupServerConnection];

    //        [sharedNetworkManager.reachabilityManager startMonitoring];

    //

    //    });

    //

    //    return sharedNetworkManager;

}

– (void)hitAPIuserRegistrationParameters:(NSDictionary *)dictionaryParameters currentView:(UIView *)view completionHandlerSuccess:(void (^)(NSDictionary *dictionaryResponse))success completionHandlerFailure:(void (^)(NSError *error))failure{

    NSLog(@”%@”,dictionaryParameters);

    if ([Utility_ObjectiveC isInternetConnected_ShowPopupIfNotConnected:true]){

        [MBProgressHUD showHUDAddedTo:view animated:true];

        self.manager = [AFHTTPSessionManager manager];

        self.manager.responseSerializer = [AFJSONResponseSerializer serializer];

        [self.manager.requestSerializer setValue:@”0a04775a6632aa79877659e5e6cd00dda67bf937_020254001488365421″ forHTTPHeaderField:@”Auth-Token”];

        [self.manager GET:[NSString stringWithFormat:@”%@%@”,BASE_URL,@”mobileapi/product_detail”] parameters:dictionaryParameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {

            [MBProgressHUD hideHUDForView:view animated:true];

            NSLog(@”JSON: %@”, responseObject);

//            BaseClass *baseClassObject = [[BaseClass alloc]initWithDictionary:responseObject];

//            NSLog(@”%@”,baseClassObject);

//

//            NSLog(@”%@”,baseClassObject.data);

//            NSLog(@”%@”,[[baseClassObject.data objectAtIndex:0] valueForKey:@”productId”]);

            ProductDetailDataModel *productDetailDataModelObject = [[ProductDetailDataModel alloc]initWithDictionary:[responseObject valueForKey:@”data”]];

            NSLog(@”%@”,productDetailDataModelObject);

            success(responseObject);

        } failure:^(NSURLSessionTask *operation, NSError *error) {

            [MBProgressHUD hideHUDForView:view animated:true];

            NSLog(@”Error: %@”, error);

            [Utility_ObjectiveC showAlertControllerwithTitle:nil Message:[error localizedDescription]];

            failure(error);

        }];

    }

}

@end

ViewController.m

#import “APIManagerNew.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    /*

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager GET:@”http://shagunn.info/cherishws/mobileapi/listMenus” parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        NSLog(@”JSON: %@”, responseObject);

    } failure:^(NSURLSessionTask *operation, NSError *error) {

        NSLog(@”Error: %@”, error);

    }];

     */

    NSDictionary *dictionaryParameter = @{@”product_id”:@”4147″,

                                          @”custom_id”:@”SI-IJ”,

                                          @”gcolor”:@”Yellow”,

                                          @”size”:@”7″,

                                          @”customer_id”:@”102″,

                                          @”metal_purity”:@”18″};

    NSLog(@”%@”,dictionaryParameter);

    [[APIManagerNew sharedAPIManager] hitAPIuserRegistrationParameters:dictionaryParameter currentView:self.view completionHandlerSuccess:^(NSDictionary *dictionaryResponse) {

        NSLog(@”%@”,dictionaryResponse);

        ProductDetailDataModel *productDetailDataModelObject = [[ProductDetailDataModel alloc]initWithDictionary:[dictionaryResponse valueForKey:@”data”]];

        NSLog(@”%@”,productDetailDataModelObject);

    } completionHandlerFailure:^(NSError *error) {

        NSLog(@”%@”,error);

    }];

    // Do any additional setup after loading the view, typically from a nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

Download sample project

Leave a comment