title: iOS and Consuming WCF service method using Json for Request/Response
body: <p>I've seen various examples on the internet of consuming WCF services using json, however haven't seen a complete example of consuming it using iOS, nor been successful at creating a simple json call to a wcf service with iOS.</p>
  
  <p>I have a service interface in wcf:</p>
  
  <pre><code>[ServiceContract(Namespace = "")]
  [DataContractFormat(Style = OperationFormatStyle.Document)]    
  public interface IService
  {
      [OperationContract]
      [WebInvoke(Method = "POST",
          BodyStyle = WebMessageBodyStyle.WrappedRequest,
          RequestFormat = WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "referenceData")]
      string GetReferenceData(string referenceName);
  }
  </code></pre>
  
  <p>On my class implementation I have these attributes also:</p>
  
  <pre><code>[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehaviorAttribute(IncludeExceptionDetailInFaults = true)]
  </code></pre>
  
  <p>I attempted to retrieve from iOS with:</p>
  
  <pre><code>NSArray *keys = [NSArray arrayWithObjects:@"referenceName", nil];
  NSArray *objects = [NSArray arrayWithObjects:@"test", nil];
  NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
  
  NSData *jsonData = nil;
  NSString *jsonString = nil;
  
  if([NSJSONSerialization isValidJSONObject:jsonDictionary])
  {
      jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
      jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
      NSLog(@"%@", jsonString);
  }
  
  NSURL *url = [NSURL URLWithString:@"http://test.com/Service.TestService.svc"];    
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  
  [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
  [request setValue:jsonString forHTTPHeaderField:@"json"];
  [request setHTTPMethod:@"POST"];
  [request setHTTPBody:jsonData];
  
  NSError *errorReturned = nil;
  NSURLResponse *theResponse =[[NSURLResponse alloc]init];
  NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;theResponse error:&amp;errorReturned];
  if (errorReturned) {
  
  }
  else {
      NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  
      NSLog(@"%@", response);
  }
  </code></pre>
  
  <p>Could someone point me in the right direction? When I test my service using WCF Test Client, I get back a json response, though when I run my call from iOS code, there is no error thrown, and my response is just empty.</p>