Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Objective-C JSON-RPC Implementation

branch: master
Octocat-spinner-32 DSJSONRPC Version 1.5 January 01, 2013
Octocat-spinner-32 Demo Version 1.5 January 01, 2013
Octocat-spinner-32 .gitignore Version 1.1.1 July 17, 2012
Octocat-spinner-32 README.md Updated documentation. January 01, 2013
README.md

Demiurgic JSON-RPC for Objective-C (iOS, Mac)

Demiurgic JSON-RPC is a lightweight and easy to use wrapper for making JSON-RPC 2.0 calls in Objective-C.

We're working to provide better documentation for the API. If you need assistance please feel free to contact us and we'll be glad to help!

Requirements


  • iOS 5.0 or greater
  • XCode 4.4 or greater

Installation


  1. In your XCode Project, add all the .h and .m files from the Source folder into your project.
  2. Import DSJSONRPC.h in any file where you wish to make JSON-RPC web service calls.
  3. Starting consuming web services without all the hassle of managing the communication yourself!

Usage


Setting up the instance

First import the header file DSJSONRPC.h in the file you'd like to do web service calls from.

Once its included, simply create a new instance of the class in one of two ways:

1) DSJSONRPC *jsonRPC = [[DSJSONRPC alloc] initWithServiceEndpoint:(NSString *)serviceEndpoint]; Service endpoint is the location of the web service where all calls will be made.

Example:
    DSJSONRPC *jsonRPC = [[DSJSONRPC alloc] initWithServiceEndpoint:[NSURL URLWithString:@"https://www.yourwebsite.com/MyWebservice.php"];

2) DSJSONRPC *jsonRPC = [[DSJSONRPC alloc] initWithServiceEndpoint:(NSString *)serviceEndpoint andHTTPHeaders:(NSDictionary *)httpHeaders; The same as above, however you can specify customer HTTP headers to include with your request.

Example:
    NSDictionary *customHeaders = [NSDictionary dictionaryWithObjectsAndKeys:@"MyApplication/1.00", @"User-Agent", nil];
    DSJSONRPC *jsonRPC = [[DSJSONRPC alloc] initWithServiceEndpoint:[NSURL URLWithString:@"https://www.yourwebsite.com/MyWebservice.php" withHTTPHeaders:customHeaders];

Making a web service call

Demiurgic JSON-RPC can be used in one of two ways. The first is to use a more traditional approach with delegates, or with completion handler blocks. Using completion handlers is our recommendation for any new applications.

Lets assume our web service has a method called echo. It takes in dictionary with the key "message", which is a string that will be echoed back to the client.

1) Completion Handler Based

[jsonRPC callMethod:@"echo" withParameters:[NSDictionary dictionaryWithObjectsAndKeys:@"Hello, World!", @"message", nil] onCompletion:^(NSString *methodName, NSInteger callId, id methodResult, DSJSONRPCError *methodError, NSError* error) {
    // Use the server response data here.  You should always ensure that methodError and error are nil first, and handle them accordingly.
}];

2) Delegate Based

[jsonRPC callMethod:@"echo" withParameters:[NSDictionary dictionaryWithObjectsAndKeys:@"Hello, World!", @"message", nil]];
// All responses will be given to the delegate specified.
Something went wrong with that request. Please try again.