Skip to content

How To Build An Cocos2d iOS App Communicating With A RESTful API – The Sequence


iOS 7 is out and therefore SpriteKit. This means I will add to this series the same solution using SpriteKit. I am really curious how much work it will be using it. But at first let me describe the next step in our cocos2d implementation.

I already explained some of my code in the other posts of this series, so that we can make this round now. Let’s see how we can finally retrieve information from an WebAPI and render them for the user as soon as the information returns.

What I didn’t describe so far is my model. My model class is the BaseMonster class that contains a list of monster attributes. Such as its health, strength, offensive value… you understand. The thing is that this information live on the server and are accessible via the Web API.

Let me go over the sequence diagram here to me it clear again.

iOS and REST Http Requests

What is loading first when the user enters the combat view is the CombatUILayer which is a CCLayer class and handles everything the user can see, her touches on the screen and so on. CCLayer also provides an update class that can be scheduled or invoked.

When initialising the CombatUILayer I immediately initialise my Monsters attributes by sending the HTTP Request to my Web API. iOS supports me here by providing standard classes like NSURLConnection. My solution doesn’t use any delegation just implements some of the methods of the HTTP protocol.

In detail that means: First I invoke a message on the MonsterController


The MonsterController creates the BaseMonster class


and sends a message to it for to update its attributes

The BaseMonster class implements two blocks that are invoked in case of a successful or erroneous request. The first block assumes a NSDictionary containing the retrieved information, the second an NSError. Very important here: The block code is only executed AFTER the HTTP request did return with result (or error).

The HTTP Request message now sends out the real request over the wire

I am just using iOS standard functionality performing the request. When the request was successful it will invoke the following method asynchronously, so I don’t know when it finished. And I can’t expect its execution exactly where I defined it.

That’s it. So when my request comes back successfully, containing some JSON payload I will serialise into an NSDictionary and give it to my completed block variable.

It is defined in the header of the class the following way

Here is the crux. When the request comes back successfully, the block code defined in my BaseMonster class takes a NSDictionary as input and assigns its content to the BaseMonster attributes. This will happen at some time, not predictable.

That’s why after the request returned from my Web API, the JSON content was put into an dictionary and was send to the BaseMonster class, I call then the update method of the CombatUILayer.

It then renders the screen with the new content. Et voila – we are done!


Much of the underlying idea comes from Steve’s answer here:

It took me some time to puzzle it together as Newbie on Objective-C. So I hope this post is helpful.