![]() |
Home | Documentation |
The iOS plugin for developing client applications on iOS (iPhone and iPad) platforms
updated Tue Aug 9 2016
|
Consuming Web services on iOS (iPhone and iPad) platforms is inreseasingly in demand today. However, Xcode does not have any built-in tools that make consuming Web services easy. It typically requires knowledge of XML processing techniques to send soap requests to the Web services and then parse the returning XML results. The gSOAP toolkit that provides an automated SOAP and XML data binding for C and C++ can be used directly to develop client applications to consuming Web services on iOS platforms such as iPhone and iPad. However, it's default network connection does not leverage the advantages of network connection offered by iOS SDK, instead it uses lower level TCP sockets for network connection. This soap plugin facilitates developers to develop Web services client applications on iOS platforms without maniulating the XML processing. Moreover, it takes andvantages of network connection offered by iOS SDK and supports 3G/4G, wifi etc.
Four Xcode examples, A Simple Calculator Example (Pure C) and A GeoIPService (C++) can be found in samples/ios.
To start building Web services client applications for iPhone and/or iPad with gSOAP , you need:
Development of Web services applications on iOS is no different from the development SOAP Web services applications on Linux, Unix and Mac OS except with a soap plugin registration. Specifically, there are four steps to develop SOAP Web services client applications on iOS:
To register the iOS plugin, simply call soap_register_plugin method:
The plugin is destroyed automatically by the soap context when the soap_end method is executed.
The interaction beween the client and the Web servcie server can be controled by specifying the cache policy. To specify the cache policy, call the method soap_ios_setcachepolicy(struct soap *soap, unsigned int policy) . The method cannot be called before the pluging is registered.
The available cache policies that can be specified are:
If this method is not called, the cache policy is set to NSURLRequestUseProtocolCachePolicy (0) by default.
The timeout interval for network connection can be specified using the method soap_ios_settimeoutinterval(struct soap *soap, double seconds). The method cannot be called before the pluging is registered. If this method is not called, the timeout interval is 60 seconds by default.
A server may request user authentication and deny access (HTTP 401 error) when the client tries to connect without HTTP authentication (or with the wrong authentication information).
HTTP authentication (basic) is enabled at the client-side by setting the soap.userid and soap.passwd strings to a username and password, respectively.
Be aware of that both of userid and password must be set in pair, that is, if one of them is set, the other must be set too.
If the server requires basic HTTP authentication, for example, the userid and password can be specified:
This section intoduces two examples to demonstrate the development of client applications consuming SOAP Web services on iOS platforms (iPhone and iPad) using gSOAP tools and the iOS plugin. Because gSOAP supports development for both pure C and C++, the first example, A Simple Calculator Example (Pure C), is developed using pure C. The second one, A GeoIPService (C++), is developmed using C++.
We assume you already have had the expereince in development of applications for iPhone and iPad using Xcode with iOS SDK installed.
It is crucial to follow these directions in order for your app to work:
This example shows you how to develop a client application in C using gSOAP and the ios plugin, which consumes a simple caculator service on iOS using gSOAP. The simple calculator service was developed and deployed as a demo using the gSOAP tools (refer to http://www.genivia.com/Products/gsoap/demos/index.html for details). The gSOAP Calculator Service provides several operations such as add, sub, mul, div etc. In this example, we use operation add as a demo. Other operations are alled in a similar way. The wsdl file for this service can be obtained at the following link: http://www.genivia.com/calc.wsdl
The Xcode project for this example can be found in samples/ios/calc.
To generate codes for the calculator Web service, we first run the wsdl2h tool from the command line on the URL of the WSDL and use option -o to specify the output file (Alternatively, you can download the calc.wsdl and use the local file instead of the URL):
This generates the calc.h service definition header file with service operation definitions and types for the operation's data. By default, gSOAP assumes you will use C++ with STL.
We have not yet generated the stubs for the CAPI. To do so, run the soapcpp2 compiler:
Option -CL indicates client-side only files (soapcpp2 generates both client and server stubs and skeletons by default). This generates a number of source files for client application development.
Launch the Xcode, create a View-based Application project and name it Calc. Open the CalcViewController.xib file in the Interface Builder. Double-click on the View item and populate it with the views listed below and shown in Figure 1:
In Xcode, edit the file CalcViewController.h to make it look like the following:
Link the op1 and op2 to the two Text Fields and delegate the button action to method buttonPressed.
Add the source files soapC.cpp, soapClient.cpp, soapH.h, and soapStub.h generated in Step 1: Generating stubs for C API to the project. Also add files stdsoap2.h and stdsoap2.cpp to the project from gsoap package as well as the iOS pluhin files gsoapios.h and sgsoapios.mm.
Once all of your files have been added to the project, ensure they are all of type "Objective C++ Source". This ensures that there will be no issues with mixing Objective C and C++ code.
In Xcode, edit the file info.plist to make it look like the following:
Then, implememt the source file CalcViewController.mm as the following:
A screen snapshot of the client is shown in Figure 2.
GeoIPService is a live SOAP Web service that enables you to look up contries by IP address or by Context.
This example shows you how to develop a client application in C++ using gSOAP and the ios plugin, which consumes the GeoIPSerive on iOS using gSOAP. The WSDL file for this servive can be downloaded at the following link: http://www.webservicex.net/geoipservice.asmx?WSDL
It is crucial to follow these directions in order for your app to work:
The Xcode project for this example can be found in samples/ios/GeoIPService.
To generate codes for the GeoIPSerbice Web service, we first run the wsdl2h tool from the command line on the URL of the WSDL and use option -o to specify the output file (Alternatively, you can download the GeoIPService.wsdl file and use the local file instead of the URL):
This generates the GeoIPService.h service definition header file with service operation definitions and types for the operation's data. By default, gSOAP assumes you will use C++ with STL.
To generate the stubs for the C++ proxy classes, run the soapcpp2 compiler:
Option -j tells the compiler to generate C++ proxy and option -CL indicates client-side only files (soapcpp2 generates both client and server stubs and skeletons by default). Option -I is needed to import the stlvector.h file from the import directory in the gSOAP package to support serialization of STL vectors.
Create a View-based Application project and name it GeoIPService and rename the .m source files to .mm. Open the GeoIPserviceViewController.xib file in the Interface Builder. Double-click on the View item and populate it with the views listed below and shown in Figure 3:
In Xcode, edit the file GeoIPServiceViewController.h to make it look like the following:
Set the ipAddress outlet and the buttonFindCountry:(id)sender method to delegate action of the button.
In Xcode, edit the file info.plist to make it look like the following:
Then, implememt the source file GeoIPServiceViewController.mm as the following:
ViewController.mm calc
Created by Bethany Sanders on 7/11/16. Copyright © 2016 Bethany Sanders. All rights reserved.
#import "ViewController.h" #include "soapGeoIPServiceSoapProxy.h" #import "soapStub.h" #import "gsoapios.h" using namespace std;