XML-RPC on IPhone

Hi, after play along to implement XML-RPC on IPhone finally I made it. I tried googling to find some articles that related to XML-RPC on IPhone, and those article drag me to Wordpress application for IPhone. This application using XML RPC to communicate IPhone with the Wordpress Blog, so the idea is grab the XML-RPC function from the WP application and implement it on our application. First we need to download Wordpress for IPhone source code, you can download it here.

Once you download the source code, extract it. You will find a folder named Class, under the Class folder you will find XMLRPC folder. We need to import this folder (with the file inside) to our project, and also import NSString+XMLExtensions.h and NSString+XMLExtensions.m.

I will show you how to implement this XML-RPC on simple application.

Start your XCode program, make new “View-based Application” project.

Picture 1

Name the project with “XMLRPC-sayHello”.

Picture 2

Once the workspace opened, import the XMLRPC folder from Wordpress source code to our project. Right click on Class folder, Add->Existing files

Picture 3

Then browse to the Wordpress for Iphone source code, and select the XMLRPC folder. Click the “Add” button. The next is import the NSString+XMLExtensions.h and NSString+XMLExtensions.m files to our project.

Your project files will be look like this.

Picture 4

Ok the import step is done, now we need to design UI. We will make a simple interface with Label and Button only. The final design will be look like this.

Picture 5

To design the UI, double click the XMLRPC_SayHelloViewController.xib file and the interface builder will show up. On interface builder Drag a label from the library and change the text to “Response”. Next drag the Round Rect Button, and change the text to “Get Response”. Once done, close the interface builder and back to XCode.

Coding

Open the XMLRPC_SayHelloViewController.h, we will import the header files that used for XMLRPC, that are XMLRPCResponse.h, XMLRPCRequest.h, and XMLRPCConection.h. We also declare the label and the getResponse method in our header file. It should be look like this.

Picture 7

Once you done with the XMLRPC_SayHelloViewController.h, double click the XMLRPC_SayHelloViewController.xib to reopen the interface builder, we will connect the code with the interface. Open the Inspector window first Tools->Inspector.

Click the Response label in designer, then in the Inspector window it will be shown “New Referencing Outlet”. Drag the right circle of “New Referencing Outlet” to File’s Owner. See image.

Picture-8

When you release the mouse button, choose the “lblResponse” from the pop up window.

Picture 9

Ok, now select the Button on the interface, and drag the right circle button on the “Touch Down” inspector window to File’s Owner.

Picture 10

Once you finished editing the interface, save it and we continue with the coding.

Open the XMLRPC_SayHelloViewController.m and add the following code.

Picture 11

You can change the server path to your own path, and don’t forget to enable the XMLRPC on your wordpress installation. On wordpress XMLRPC there is demo.sayHello method that returns “hello” string, so I used this for the example.

Tadda! It’s done! Run the emulator by pressing command+enter button. Once running, press the “Get Response” button, that will invoke the getResponse method and show up the result to lblResponse.

Download source

No related posts.

12 Comments

  1. Lex says:

    Hi Leo,

    nice tutorial. I implemented an example which is very much like yours to see if I can connect to my server. There seems to be a problem in the last line, [userInfoResponse object] – this throws a “NSInvalidArgumentException, reason: *** [NSError object]: unrecognized selector sent to instance …” error. Did you have this to? Any ideas why?

  2. Ohtman says:

    Hi
    very nice tutorial ,thanks
    I get the same error as Lex said, but i found a solution :
    the probleme was just a wrong parameter
    try with this adress : @”http://testdean.wordpress.com/xmlrpc.php”

    :D

  3. ad says:

    Hi, I’m trying to develop an application based on your post, and on Kilbot’s one, but I’m doing something wrong. My app has an exception everytime.

    Terminating app due to uncaught exception ‘NSInvalidArgument
    Exception’, reason: ‘*** +[NSString decodeXMLCharactersIn:]: unrecognized selector sent to class 0×30692800′

    I debugged and found that it crashes on the first line of this function:

    - (id)decode
    {
    [_parser parse];

    NSError *parseError = [_parser parserError];
    if (parseError) {
    return parseError;
    }

    return decodedValue;
    }

    Even though, I still don’t know what I’m doing wrong. Some advice would be very helpful.

    Thanks!

  4. ad says:

    Well, I tried to connect by modyfying your source instead of starting a new one and it works. But…

    - I tried demo.multiplyTwoNumbers with Ohtman’s address, and I get back a NSDictionary but its response is an error, faultString and faultCode. Where’s the issue? Is it permission problem? I send the numbers as strings in the args array, is that correct?

    - I wonder why the button doesn’t act properly. Is there a fix for that?

    Thanks

  5. ad says:

    Hello, I’m trying to develop an application like yours, and I have some doubt:

    - It works fine when trying the method demo.sayHello, but when I try demo.multiplyTwoNumbers I get back a NSDictionary with 2 keys, but they are not the result, they are failString and failCode, or sth like that. I’m using Ohtman’s address and sending the parameters as strings in the array. Where’s the issue?

    - I’d like to know why the “Get Response” button doesn’t act properly when u press it. Just curiosity.

    Thanks!

    • leoganda says:

      You were right, pass the parameters as string, then in the server just parse it as integer or something. Return the result value as string too.

      I don’t understand with the button you’re talked about. What doesn’t act properly?

  6. ad says:

    Sorry for repeating the post. When I run your application on the simulator, the button doesnt change its colors when I click it. It has some kind of lag.

  7. Othman says:

    HI all
    I’ve an other probleme, whene i excute my code with an auther server (my server) whritten in java, i received this Exception in the response :

    java.lang.NoSuchMethodException : …myMethode(int, int)

    is there any one who can give me a response to my probleme please :)

  8. ad says:

    Thanks, guys.

    Now I kinda succeded in establishing a communication between my app and my php WS, but I have some problems with types.

    I created the WS using phpxmlrpc library. Do you know if it’s compatible with the wordpress library? I sometimes find that I dont get returned a NSDictionary but a String.

    I also would like to know how to send integers or other types to the WS, not only strings.

    Thanks!

  9. Hi guys,

    This article just helped me out.

    Thanks a lot !!

Leave a comment