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

32 thoughts on “XML-RPC on IPhone”

  1. 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. 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 0x30692800’

    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!

    1. Hey guys,

      I solved my “+[NSString decodeXMLCharactersIn:]: unrecognized selector sent to class 0x30692800” error. The problem was my file hierarchy and import of the files “NSSTring+XMLExtensions.h” and “NSString+XMLExtensions.m”

      The error occurred, when I put both files under classes/XMLRPCLIB/. My XMLRPC Classes were under classes/XMLRPCLIB/XMLRPC/ separately.

      so, I put the “NSSTring+XMLExtensions.h” files under “Classes/” and this solved the problem.

      So keep your file hierarchy like it is shown on picture 4 🙂

      This error took me 2 days to solve. I hope, that you can keep around this debugging time.

  3. 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

  4. 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!

    1. 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?

  5. 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.

    1. I noticed same issue with the button. I don’t know exactly, but maybe the problem is on the emulator.

  6. 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 🙂

  7. 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!

  8. the services I am trying to connect to require authentication (username/password) Does anyone know if this library supports this? If so can someone post an example. Thanks!

    1. XML-RPC doesn’t support anything like HTTP authentication AFAIK, but if you look at some of the methods that WP supports, they have you pass a username and password value in the parameter array.

      For instance:

      [req setMethod:@”mt.getRecentPostTitles” withObject:[NSArray arrayWithObjects:@”1″, @”username”, @”password”, nil]];

  9. Hi, thanks for posting this tutorial!

    Ive downloaded your source code and when i build and go, i get a warning next to the line of code:

    NSString *result=[self executeXMLRPCRequest:reqHello];

    The warning says:

    ‘XMLRPC_SayHelloViewController’ may not respond to ‘-executeXMLRPCRequest:’ (Messages without a matching method signature will be assumed to return ‘id’ and accept ‘…’ as arguments.)

    When i click the “Get Response” button in the simulator, the program just crashes out.

    Has anyone got any ideas??

    Thanks

    Dave

    1. Similar issue, I downloaded the source, changed the url and args to a working xmlrpc service i have here, and ran it and it crashed. I’m running 3.1.2.

  10. Also found something else. Got it to work but if you return Strings it crahses, if you return NSDictionaries they are empty with no value. I think the problem may be in the decoder becauese I can trace that the service is returning values correctly… its when the client is trying to decode the response that it runs into issues.

    any help would be appreciated.

  11. Thank you for the code, I couldn’t fin any good source about XML-rpc on the iphone that worked properly on the new 3.2 and 4.0 release.

  12. the code doesnt work for me. it seems the XMLRPC in wordpress got updated. filestructure is different, etc. This here needs updating… Unless I take your code with an older version….

  13. I have used the same code as above and get warning:
    ‘MyViewController’ may not respond to ‘-executeXMLRPCRequest:’
    at this line of code:
    NSString *response = [self executeXMLRPCRequest:request];

    And when running getting this error:
    -[NSURLError object]: unrecognized selector sent to instance 0x5f582d0
    2010-10-10 21:42:16.876 CBH[14250:207] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSURLError object]: unrecognized selector sent to instance 0x5f582d0’

    Any Suggestion???? I am stuck

  14. Hi Guys,

    This is a very useful article, although for me it complains about CTidy. I added those classes as well, but still can’t find its declaration. Did anybody find a solution for this problem?

    1. Having the same issue as Peter. I added the CTidy.h in the same place as it is under the WordPress 2.8. Same Error. Also, getting an error about one of the #defines in Constants.h. Same problem. I added Constants.h and it still gives me the error.

  15. I am using this XML RPC code to make synchronus calls.Is it possible to make Asynchronus calls ?

  16. how to fetch data from wsdl-rpc into tableview for iphone sdk…..?Plz anyone tell me or send me some example code for that…!

  17. Perfect, thank you.

    Got error:
    [__NSArrayM (or other class) isEqualToString:]: unrecognized selector sent to instance

    How To Fix:

    in -(IBAction)getResponse:(id) sender{

    CHANGE FROM

    if( ![result isKindOfClass:[NSString class]] ) //err occured.
    lblResponse.text=@”error”;
    lblResponse.text=result;

    TO

    if( ![result isKindOfClass:[NSString class]] ) //err occured.
    lblResponse.text=@”error”;
    else
    lblResponse.text=result;

Comments are closed.