I've implemented the AFNetworking framework, however I was trying to figure out a way of having the AFJSONRequestOperation function in a separate class and calling it from my individual view controllers.
What I've done is create a class method that takes the params in a dictionary and the webservice url. I would like this to be able to return the JSON array and the Http Code from the success block, however this is not working.
If I change the public function to have a return type and return a value at the end of the method, it returns before the success block has completed.
Any suggestions?
+ (void)RequestJSON:(NSDictionary *)params:(NSString *)webServicePath {
NSURL *url = [NSURL URLWithString:@"http://api.website.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:webServicePath parameters:params];
NSLog(@"%@", request);
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSString *httpCode = [[JSON valueForKey:@"meta"]valueForKey:@"code"];
NSLog(@"code=%@", httpCode);
}
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
{
NSLog(@"Failed: %@",[error localizedDescription]);
}];
[operation start];
}