JSON WebServices from iOS in Objective-C

In this article I present an effective way to call a webservice method from an iOS device.

Input and output are encoded to and decoded from a widely used format: JSON. For this I use the NSJSONSerialization class included in iOS starting from version 5.0.

First of all prepare input arguments by using an NSDictionary. This is an initialization valid for a fixed number of input arguments. The sample has 3 input arguments that may be any of the allowed NSJSONSerialization types (see NSJSONSerialization documentation).

    NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:in1, @"Input1", in2, @"Input2", in3, @"Input3", nil];

The conversion to a JSON string is a two step process . First to a binary format (NSData) by using dataWithJSONObject:options:error:, then to the JSON NSString. The error handling is not performed in this sample.

    NSError *error = nil;
    NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];

the synchronous WebService call , is a HTTP POST call to the server.

    NSURL *url = [NSURL URLWithString:@"http://host:port/Service.asmx/WebService1"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

The execution will block on sendSynchronousRequest:returningResponse:error: until the full response is read from the server.

The JSON (binary) response data from http call can be converted into the corresponding Objective-C object by using:

    // Parse response
    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

I use ‘id‘ reference because usually we do not know which is the object type returned from the server. Here the sample may be considered completed, but I want to add additional tips to interpret the output and detect error messages that may come from the server.

Even if we know the type returned bye the server it is a better choice to check it because the server may return an error in a JSON response which will be encapsulated in a different object type.

The .NET server on which I tested this sample code always returns a collection of name/value pairs, decoded by NSJSONSerialization into an NSDictionary Objective-C object. This collection always contains a value named “d”. So I check for it and extract the value of “d”:

    NSDictionary *jsonResponseDict;
    if ([jsonResponseData isKindOfClass:[NSDictionary class]]) {
        jsonResponseDict = jsonResponseData;
    } else {
        // Error-handling code
    jsonResponseData = [jsonResponseDict objectForKey:@"d"];
    if (jsonResponseData == nil) {
        // Server may have returned a response containing an error
        // The "ExceptionType" value is returned from my .NET server used in sample
        id jsonExceptioTypeData = [jsonResponseDict objectForKey:@"ExceptionType"];
        if (jsonExceptioTypeData != nil) {
            NSLog(@"%s ERROR : Server returned an exception", __func__);
            NSLog(@"%s ERROR : Server error details = %@", __func__, jsonResponseDict);

If the server does not return expected values you can debug by logging jsonResponseDict value.

Tip: By using “__func__” in NSLog you can easily track the class/method where logged message was written.

From now on you can iterate by examining the jsonResponseData object type and extracting data from it. JSON definition declares only two collection types: “A collection of name/value pairs”, which is mapped to NSDictionary, and “An ordered list of values” which is mapped to NSArray. You can iterate on items of these collections and extract basic types (NSString, NSNumber).