This tutorial outlines how to call Soap Web Services from iOS or Mac OS XCode Projects in just few steps using Native Objective-C libraries and additional source code provided by WSClient++.
Code Generation
First step is to generate code from the available wsdl location. WSDL is Web Service Description Language specified in XML, which allows us to interpret schema used in transfer of objects between local and remote endpoints. To generate code, open WSClient++ and create a new configuration file which will specify WSDL URL, Folder to store generated files and select type as Objective-C and choose iOS or Mac OS as per your choice. As shown in following picture.
Generated Code
WSClient++ generates following sets of classes as below. So lets assume, following simple Inventory Web Service that allows us to Add/Delete/List products.
Synchronous Web Service Client
Synchronous Web Service Client class will be named as “NamedService”, which in our case will be “InventoryService”. Which will have synchronous methods which will make soap calls to destination server and will return the native representation of soap response in terms of custom types or native types as described by the WSDL. Synchronous client will wait for method call to finish, however it may return an error in case of timeout. But this may halt the user interface so we recommend using Asynchronous Web Service Client for most practical purposes.
Asynchronous Web Service Client
Asynchronous Web Service Client class will be named as “NamedServiceAsync” which in our case will be “InventoryServiceAsync”. It will have all methods same as that of Synchronous Web Service Client, but these methods will return nothing. Instead you will have to set a delegate which will have implementations of expected protocol, that will have actions for each method’s response and OnError.
Request/Response and Custom Types
For every custom xml type, WSClient++ will generate corresponding Objective-C class and it will marshal every request in xml as expected by Soap. You can simply invoke and access these objects as your other Objective-C classes, and NSWSDL.* files will do xml parsing and processing for you in background.
Invoking Web Service
After including generated files and NSWSDL.* (and NSXMLDocument.* for iOS) in your project, you will be able to access web service in your project as shown below.
For Asynchronous calls, please define following methods which will implement responses of your web service and this class will be the delegate passed to web service.
- -(void) onError: (NSError*) error{
- // … process error…
- }
- -(void) onCreateProduct:
- (InventoryServiceAsync*)service
- result:(Product*) r{
- //… process result… you will get returned header here…
- }
In the same class, we will call method to invoke the service as below,
- -(void) callService{
- InventoryServiceAsync* ws = [InventoryServiceAsync service];
- // set base url for entire application
- [BaseWebService setGlobalBaseUrl: @""http://domain.com" ];
- // //set base url only for this service
- // [ws setBaseUrl: @"http://domain.com"];
- [ws setDelegate: self]; // dont forget to set delegate
- [ws CreateProduct: @”Sample”];
- }
So you see, calling Soap Web Service is very easy as you are calling your other native Objective-C API.
Download today your free trial at http://wsclient.neurospeech.com