Friday, 3 February 2017

AFNetworking - How to handle HTTP status error codes and messages in failure block?

failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 NSLog(@"Failure: %@", error); 
 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;  
 NSLog(@"%zd", httpResponse.statusCode); 
 id errorJson = [NSJSONSerialization  JSONObjectWithData:error.userInfo[AFNetworkingOperationFailingURLResponseDataError  Key] options:0 error:nil];
 NSDictionary *errorJsonDict = (NSDictionary *)errorJson; 
 if (!errorJsonDict)    
    return;
 if ([errorJsonDict isKindOfClass:[NSDictionary class]] == NO) 
     NSAssert(NO, @"Expected an Dictionary, got %@",NSStringFromClass([errorJsonDict  class])); 

  NSLog(@"%@",errorJsonDict.description); 
}

No comments:

Post a Comment