-3

I'm trying to get this UIAlertView to run when fetching the JSON data fails, but i can't seem to get it to work. I'd appreciate it if someone could show me how to do it or point me in the right direction!

Jgreen727
  • 75
  • 9

2 Answers2

0

I think you have forget to write failure block.

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success: %@", operation.responseString);
} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);
}];

Hope this will help you.

Kirti Nikam
  • 2,166
  • 2
  • 22
  • 43
  • I have a failure block already, i just didn't add it in here because it was irrelevant! The failure/success blocks only account for fundamental errors. – Jgreen727 Jun 29 '15 at 18:20
  • Ok, just now checked your comment. Check my comment below your question. – Kirti Nikam Jun 29 '15 at 18:22
0

You can write this in your block

NSDictionary *headersCollection=[[(NSDictionary *)operation valueForKey:@"response"]valueForKey:@"allHeaderFields"];
NSMutableDictionary *headers=[headersCollection mutableCopy];
headers[@"statusCode"]=[NSNumber numberWithInteger:operation.response.statusCode];

and then check the status code value. Headers dictionary will provide you the complete header which is returned. If statuscode is 200 everything is OK else you can show your custom message accordingly with different status code values like 400, 415 etc

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
  • What does this have to do with the question? – rmaddy Jun 29 '15 at 18:19
  • 1
    well u can do anything when u get the header and statuscode. As in the question it is not mentioned how json fails. It can fail in any context. Secondly if you want to pass this header dictionary to some other controller where you are executing the web service call, you can make the success block there and pass the dictionary of headers – Rajan Maheshwari Jun 29 '15 at 18:22