Consume WCF Web Service using Objective-C on iPhone

ghz 7months ago ⋅ 123 views

I am having a hard time consuming a very simple (Hello World) WCF web service in my iPhone app. From what I have read, you must manually create the request message then send it to the web service URL.

I was able to accomplish this on a .asmx web service, but not with a WCF service.

How do I know the correct format of the request SOAP message?

The web service I am trying to hit has a format of: http://xxx.xxx.xxx.xxx:PORT/IService1/ (running locally in a VM)

I apologize for the lack of information, I am pretty lost.

Any and all help is much appreciated.

Answers

Consuming a WCF web service from an iOS app involves creating and sending SOAP requests to the service endpoint. Here's a step-by-step guide to help you:

  1. Understand the WCF Service Contract: Ensure you understand the operations (methods) exposed by the WCF service, including their parameters and return types.

  2. Generate SOAP Envelopes: SOAP is a protocol for exchanging structured information in web services. You need to create SOAP envelopes to send requests to the WCF service. These envelopes typically include the SOAP envelope, header, body, and appropriate namespaces.

  3. Send SOAP Requests: Once you have the SOAP envelopes, you need to send them to the WCF service endpoint using HTTP POST requests. You can use libraries like NSURLSession or NSURLConnection to send HTTP requests from your iOS app.

  4. Handle SOAP Responses: After sending the request, you'll receive SOAP responses from the WCF service. You'll need to parse these responses to extract the data returned by the service.

  5. Error Handling: Implement error handling to handle cases where the request fails or the service returns an error response.

Here's a simplified example of how you might send a SOAP request using NSURLSession:

// Create SOAP envelope
NSString *soapMessage = @"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://example.com/service\">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:YourMethod>
         <!-- Include method parameters here -->
      </ser:YourMethod>
   </soapenv:Body>
</soapenv:Envelope>";

// Set request URL
NSURL *url = [NSURL URLWithString:@"http://xxx.xxx.xxx.xxx:PORT/IService1/"];

// Create request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"YourSOAPAction" forHTTPHeaderField:@"SOAPAction"];
[request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

// Send request
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // Handle response here
}];
[task resume];

Replace YourMethod with the name of the method you're calling, and update the SOAP envelope with the appropriate method parameters.

Keep in mind that WCF services can have various configurations, such as authentication, message encoding, and transport protocols. Make sure your iOS app handles these configurations appropriately based on the requirements of the WCF service you're consuming.