iOS REST-JSON APIs

Now that I seem to be getting the hang of iOS development I want to try and get some cool integration with PaaS and BaaS services from various places on the internet. Perhaps some facebook integration, or perhaps building a slick iOS client for my blog that can do something beyond what a responsive client could do. I noticed that BestBuy has an open API – perhaps a connection to them querying on any products I review that are in their store?

There’s not too much in the way of examples out there when you google around – and there’s not much in the sample iOS applications that Apple has. So what’s a developer with a week off form work supposed to do with this time? Relax? Yes! Exactly – so let’s grab some beers and maybe a decent whiskey (along with a few espresso’s) and dig in!

RestKit.org – a great start!

On iOS there do not seem to be too many libraries or frameworks that simplify working with REST/JSON interfaces, but there is one – restkit.org. I grabbed this kit and after reading thru the docs decided to give restkit.org worksthis one a shot. The docs were a little sparse – but after scratching my head and thinking “these install instructions can’t possibly work”, I gave it a shot. Holy smokes! They worked! Amazing! The best version to take a look at is the set of instructions on github – there’s a link to them from the main site.

One disappointing thing is that none of the examples seem to work Correction: I’m not sure what was wrong, but they do in fact work for me – I’m using the very latest Xcode, iOS SDK, and OS-X (4.5.2, 6.0, 10.8.2). The examples all seem to start ok, but nothing ever loads up in the simulators. I performed a full clean and build – seemed to fix whatever was wrong.

The next step is to build something useful – which means I need to find a decent service that I want to play around with. I learned the other day that BestBuy has an open API for querying their data – products, locations, reviews, etc. on the stuff they share. You can signup and use it for free at bbyopen.com.

There’s some decent tools that can help you play around with REST and web APIs in general. Since I’m typing in Firefox, I’ll use Chrome to help out with this. Postman is a nice extension that can make REST/JSON calls pretty simple and save them to reuse them. Here’s a screen shot of a return from a query against the bbyopen service. Postman for ChromeWell that’s pretty nifty. You can get data in either XML or JSON formats. One thing I’m curious about – if API calls are throttled with the key then how do you keep your key from being hijacked since it is passed in the URL?

You can extract out parameters like the host name and save those as environment variables so that switching from a test to a prod server is fairly simple. Once you get the hang of it, Postman is pretty handy.

Back to iOS and RestKit

Let’s get back to the show! I’m going to create an iPad app (iOS 6.01) that can exercise some of the basic queries. I followed the basic instructions to get restkit working in my project. I instantiated the RKClient singleton in my app delegate’s didFinishLaunchingWithOptions method:

    RKClient *client = [RKClient clientWithBaseURLString:@"http://www.houseofbeor.net"];
    RKLogConfigureByName("RestKit/Network", RKLogLevelDebug);
    RKLogInfo(@"Configured RestKit Client: %@", client);

From this code you can see in the output logs that things seem to be working ok, but of course it does not do much. Let’s try something different – twitter.com has a decent REST API that is open to messing around with. Its a good case for just testing the URL connections and that the pipes are working. Using the same basic app, I’ll change up the functions in the app delegate to look like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RKLogConfigureByName("RestKit/Network*", RKLogLevelTrace);
    RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

    RKClient *client = [RKClient clientWithBaseURLString:@"http://www.twitter.com"];
    RKLogConfigureByName("RestKit/Network", RKLogLevelDebug);
    RKLogInfo(@"Configured RestKit Client: %@", client);

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

I added a few logging statements so that I could see more in the output. I’ve also changed up the base url. Then I call a very basic UIViewController. In the view controller at this point all I do is add a bit to the url, call a GET method and then do some bits to handle the response:

- (void) request:(RKRequest *)request didLoadResponse:(RKResponse *) response
{
    if ([request isGET])
    {
        //handle the foo
        if ([response isOK])
        {
            NSLog(@"Retrieved XML: %@", [response bodyAsString]);
        }
    }
}

and you should be on your way. Seems toe work pretty darn good – great job to the folks at restkit.org! Now I’m going to explore getting a bit more serious with their object mappers and manager.

 

 

1 Comment

  1. Pingback: Parsing JSON and Object Mapping with RestKit » Tarn Aeluin

Leave a Reply