This is the second tutorial of Pico Tutorial series, in the first tutorial, I showed how to use Pico with a service called StockQuote from webserivceX.NET. Today I will show you how to use Pico with another service called CurrencyConverter, also from webserviceX.NET, in first tutorial, I showed you how to reference Pico as a static library, in this tutorial, I will show you how to reference the Pico source in your project. By the way, since the wsdl driven development process in both tutorials are quite similar, I won’t repeat too much details in this tutorial, I suppose you have already read tutorial one and basically understand the wsdl driven development process supported by Pico.
By default, the proxy code will be generated in the sub-folder corresponding to the target namespace of the wsdl.
There is a generated folder called client, the SOAP and XML proxy interface will be generated in this folder.
There is a generated folder called common, a common header file will be generated in this folder, the common header includes headers of all types generated from wsdl/schema, use this header file can free you from writing many import statements in your project when you build request or handle response needed by service call.
Step 2 - Create New iOS Project, Add Pico Library and Generated Proxy into Your Project
Create a new simple iOS single view application named “CurrencyConverter”, don’t choose ARC, download Pico source and drag the whole PicoSource folder into the project, choose “Copy items to destination group’s folder” and “add to targets” when prompted. Then do following settings to the project:
In Target Build Setting, add the -ObjC flag to your “Other Linker flags”.
In Target Build Setting, add /usr/include/libxml2 to your “Header Search Paths”
In Target Build Phases, link binary with library libxml2.dylib
Build the the project to ensure that it can build successfully.
Now drag the proxy generated in step 1 into the project, choose “Copy items to destination group’s folder” and “add to targets” when prompted.
Build the the project again to ensure that it can build successfully.
The finished project should look like the screen shot below:
Step 3 - Implement Appliction Logic and UI, Call Proxy to Invoke Web Service as Needed.
Now open ViewController_iPhone.xib in interface builder, then add a few UI components like following screen shot:
Add IBOutlet properties and IBAction method in ViewController.h, then wire the properties and method with UI components accordingly, bascially, the application will convert the From currency to To currency and show conversion rate, when the Convert button is clicked(which will trigger onConvertClicked method internally).
Now implement the onConvertClicked method by invoking service as below:
#import "ViewController.h"#import "CurrencyConverterSerivceClient.h"#import "SOAP11Fault.h"#pragma mark - UI Event Handlers-(IBAction)onConvertClicked:(id)sender{if(!self.fromCurrencyTextField.text.length||!self.toCurrencyTextField.text.length){UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Invalid Parameters"message:@"Please enter valid from and to currency types and try again"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];[alertshow];return;}[self.activityIndicatorsetHidden:NO];[self.activityIndicatorstartAnimating];// Get shared clientCurrencyConverterSerivceClient*client=[CurrencyConverterSerivceClientsharedClient];client.debug=YES;// enable message loggingclient.timeoutInverval=10;// http timeout in seconds// build requestConversionRate*request=[[[ConversionRatealloc]init]autorelease];request.fromCurrency=self.fromCurrencyTextField.text;request.toCurrency=self.toCurrencyTextField.text;// make API call[clientconversionRate:requestsuccess:^(ConversionRateResponse*responseObject){// success handling logic[self.activityIndicatorstopAnimating];UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Success!"message:[NSStringstringWithFormat:@"Currency Conversion Rate is %@",responseObject.conversionRateResult]delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];[alertshow];}failure:^(NSError*error,id<PicoBindable>soapFault){[self.activityIndicatorstopAnimating];// error handling logicif(error){// http or parsing errorUIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"Error"message:error.localizedDescriptiondelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];[alertshow];}elseif(soapFault){// soap faultSOAP11Fault*soap11Fault=(SOAP11Fault*)soapFault;UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"SOAP Fault"message:soap11Fault.faultstringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];[alertshow];}}];}
Please don’t forget to include the shared CurrencyConverterSerivceClient.
There are other similar demos in the Examples folder of Pico source, like the BarCode demo which calls web service that will return base64 encoded barcode data and the Weather demo which shows the weather given a zip code, see screen shots below, I won’t create tutorials for all these simple demos, since they are quite similar. Next time, I plan to show you how to use Pico with industrial level web serivces, like Amazon and eBay web serivces, just stay tuned.