Sunday, October 26, 2008

Mac to WCF Webservice - step 3

Following a substantial amount of head scratching with regard to connecting my Mac to a WCF WebService I'm finally at the stage of moving this to the iPhone environment. This application will build on the WCF service which was created in my earlier article.

  • Create a new iPhone application in XCode of type View Based Application.

  • Give it a meaningful name, I used iWCFDemo.

  • Add the following code to the iWCFDemoViewController.h file


  • @interface iWCFDemoViewController : UIViewController {
    IBOutlet UITextField *resultsField;
    }
    -(IBAction)pingTest:(id)sender;
    @end


  • Add the following code to the iWCFDemoViewController.h file


  • -(IBAction)pingTest:(id)sender
    {
    NSLog(@"Pressed!!");
    }


    This will allow for one button to invoke the WebService request and a text field to display the results following the call.


  • Double click the MainWindow.xib file to start the Interface Builder.

  • - Drag a Text Field from the Library onto the View Suface.
    - Drag a Round Rect Button onto the View Surface and give it the text "Press Me"
    - Drag a label onto the View Surface and give it the text "Results:"
    The result should look like the following:

  • Select the "File's Owner" from the xib view and drag a connection from the pingTest outlet to the button on the Layout Surface. Connect it to the "Touch Down" event.

  • Drag a connection from the resultsField Outlet to the Text Field on the Layout Surface.

  • The result should look like the following:

  • Save all the file and hit "Build and Go", when the Simulator appears, pressing the "Press Me" Button should produce the "Pressed!!" message in the Debugger Console.
    The results should look like something the following:

  • Now we add the connection to the WebService in by Generating the Proxi using WSMakeStub. Start a Terminal session go to the /Developer/Tools directory. Execute the command "WSMakeStubs -x ObjC -dir /Users/username/ -name JokeGen -url http://192.168.1.106/JokeGen/Joke.svc?wsdl" where the username is a valid location on your machine. This will produce the 4 files you need to access the WebService. Add these generated files to your XCode solution.

  • Modify the JokeGen.m file by adding the methodNamespace: @"http://tempuri.org/" and changing the Style parameter to (NSString*) kWSSOAPStyleDoc

  • Search for the resultsValue method in JokeGen.m and replace the generated with:
    - (id) resultValue
    {
    // return [[super getResultDictionary] objectForKey: @"parameters"];
    return [super getResultDictionary];
    }

    We do this so we can see Null values being passed back regardless of what type is sent via the WCF method.

  • Modify the WSGeneratedObj.m file by changing the generated code as its not correct first time out. Due to a conflict in the return values from the SOAP call we need to change the variable from "soapAction" to "soapActionKey".
    NSString* soapActionKey = @"SOAPAction";
    NSDictionary* headers = [self copyHeaderDictionary:1 extraVals:&soapAction extraKeys:&soapActionKey];

  • Add the import statements to the iWCFDemoViewController.m file, #import "WSGeneratedObj.h" and #import "JokeGen.h". Finally modify the pingTest method to invoke the WebService.

    -(IBAction)pingTest:(id)sender
    {
    NSLog(@"Pressed: ");
    NSString *result = [[JokeGen Ping:@""] objectForKey:@"/Result"];
    NSLog([result description]);
    resultsField.text = [result objectForKey: @"PingResult"];
    }


  • As these WebService calls require a number of additional functions which are not part of the standard application build so we have to add the CoreService. Select Add/Existing Framework from Frameworks folder in the Project tree. Browse to the Frameworks folder and select the CoreServices.framework and click OK

  • "Build and Go" the application and the result should be as follows:

    With the Debug window showing the full XML results for the Service Call:




OK so this is hardly an Earth shattering example, however it does show that the Mac OS and Windows .Net are valid options for developers.