How to return Array of locations for the PINCode as an input parameter with completion block of code?

144 Views Asked by At

I have a scenario where user will enter PINCode number and that number I have to pass as parameter to the Webservices API(User which is a singleton class) then I will get an array of locations as response and that response I should show in A lable below the text field of PINCode. So what I understood is I have to create a method with completion block in that method. but My doubt is how can get the array of locations to represent in label. I'm really confused and I tried this

API.h

+(void)submitPINCode:(NSInteger *)pinCode CompletionBlock:(void(^)(BOOL status, NSArray * responseArray))handler;

API.m

+(void)submitPINCode:(NSInteger *)pinCode CompletionBlock:(void(^)(BOOL, NSArray *))handler

{

    NSString *string = [NSString stringWithFormat:@"%@%@",URL,pinCode];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        BOOL status = NO;
        if (operation.response.statusCode == kStatusCodeSuccessfullResponse) {
            status = YES;
            //  here we have to send an array so make the response object in to array or create a response Dictionary as you wish

            NSArray *responseArray = (NSDictionary *)responseObject;

        }

        handler(status,BfErrorCodeUnknownError, responseArray);


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSInteger statusCode = operation.response.statusCode;
        BfErrorCode errorCode = BfErrorCodeUnknownError;
        if (statusCode == kStatusCodeNoInternetConnection) {
            errorCode = BfErrorCodeNetworkError;
        }

        NSString *errorMessage = [NSString stringWithBfErrorCode:errorCode];

        handler(NO,errorCode,nil); // here if nil is doesnt work just pass an empty array instead of nil create an array and pass it
    }];

//    [operation start];

}

So My doubt is:

1) I'm I going in a right way or what the changes I have to do to get the answer, and

2) weather I have to create Class method or Instance method ? Where I have to create class and instance methods in general for these kind of situations ?

1

There are 1 best solutions below

6
On BEST ANSWER

May be this could help you:

API.h

typedef NS_ENUM (NSInteger, WebServiceResult)
{
    WebServiceResultSuccess = 0,
    WebServiceResultFail,
    WebServiceResultError
};
typedef void(^myCompletionA)(NSDictionary *data,WebServiceResult result);
+(void)postDataDict:(NSInteger *)pinCode isLoding:(BOOL)loading WithBlock:(myCompletionA)compileBlock;

API.m

+(void)postDataDict:(NSInteger *)pinCode isLoding:(BOOL)loading WithBlock:(myCompletionA)compileBlock{
    if ([objAppDelegate connected]) {
        if (loading) {
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
        }

        NSString *urlString=[[NSString stringWithFormat:@"%@%@",BASE_URL,pinCode] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
       // manager.requestSerializer = [AFJSONResponseSerializer serializer];
        //manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        [manager POST:urlString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [SVProgressHUD dismiss];
//            NSLog(@"Response : %@",responseObject);
            if ([responseObject[@"status"] isEqualToString:@"success"])
            {
                compileBlock(responseObject,WebServiceResultSuccess);
            }
            else
            {
                compileBlock(responseObject,WebServiceResultFail);
            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSDictionary *errorDict=[[NSDictionary alloc]initWithObjectsAndKeys:error,@"error", nil];
            compileBlock(errorDict,WebServiceResultError);
            NSLog(@"%@",error.description);
            [SVProgressHUD dismiss];
        } ];
    }
    else
    {
        [objAppDelegate showMessage:@"No internet connection." withTitle:@"nil"];
    }
}