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 ?
May be this could help you:
API.h
API.m