This topic is already covered in my post for consuming .NET web services in iOS but some questions were raised about passing arguments to the JSON-RPC methods that require them. In this short post I’ll just explain how to do just that.
Assume you have a few RPC methods that require some arguments like this.
[Jayrock.JsonRpc.JsonRpcMethod("HelloWorld")] public string HelloWorld() { return "Hello World!"; } [Jayrock.JsonRpc.JsonRpcMethod("Echo")] [Jayrock.JsonRpc.JsonRpcHelp("Simple echo method, takes string input and returns it")] public string Echo(string input) { return input; } [Jayrock.JsonRpc.JsonRpcMethod("EchoArray")] [Jayrock.JsonRpc.JsonRpcHelp("Returns input arguments as an array of strings")] public List EchoArray(string str1, string str2, string str3, string str4) { List lst = new List(); lst.Add(str1); lst.Add(str2); lst.Add(str3); lst.Add(str4); return lst; } |
Now, those “Echo” methods need some arguments passed to them and in this case I worked with strings but remember those args can be any primitive or complex but serializable types. So now that we have a working web service, we can query it from iOS. I’ll refer to my old example project which can be found here. The way it’s coded right now is to call the HelloWorld method without any arguments like this.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"HelloWorld" andParams:[NSArray array] withID:@"1"]; |
We need to notice that we passed an empty NSArray for params. This is because our HelloWorld doesn’t need any arguments. Easily enough, if you want to call the Echo method with a single param you’ll just add a string to an array and pass that array as execMethod:andParams:withID 2nd argument.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"Echo" andParams:[NSArray arrayWithObject:@"Echo Me!"] withID:@"2"]; |
You can pass as many arguments as your method requires just stack ‘em up in an NSArray and make sure the argument order complies with the argument order of the web method.
JSONRPCService* svc = [[JSONRPCService alloc] initWithURL:[NSURL URLWithString:txtURL.text]]; svc.delegate = self; [svc execMethod:@"EchoArray" andParams:[NSArray arrayWithObjects:@"1st arg", @"second arg", @"3rd arg", @"4th arg string", nil] withID:@"3"]; |
Hope this helps!

















