Cocos2D Revised Template – Easy-ARC, Unit Tests, Cross-Ref’d Source

I don’t know about you guys, but on the one hand I’m grateful for all the people who make it easy to get up and running with either a UIKit based project or a Cocos2D based project in Xcode. Because I’ve just spent literally hours ripping my hair out in frustration, stuck on configuration details and so on. I hate that; trying to fumble through a task, really not knowing what causes it to fail and in the end you get it working and really not know why it did stop failing. (Talk about a cry for mentor help!)

So, this is what I want to do:

* I want the functionality of a Cocos2D template, but I want to cross-reference the Cocos2D-iOS project and not have the source there already (i.e. if I wanna update my source, it should be easy)

* I want it set up for ARC

* I want to have unit tests

Unfortunately the current templates don’t provide this. So, here’s how I got it working:

* Create a new Project, using the Cocos2D iOS Template

* Now go to the App Target’s Build Settings and notice what might be interesting here (in the event you wanted to work without templates):
– Other Linker Flags (-lz -ObjC)
– Location of the Info.plist file
– Header Search Paths (where to locate the kazmath headers)
– Location of Prefix Header
– Preprocessor Macros

* Double check some Project Build settings
– iOS Deployment target (could be 5.0 at this point)
– Objective-C Automatic Reference Counting should be set to YES, and make sure this is also the case in your App Target

* We will now delete the libs folder from the Project and send it to the Trash, but before that, notice what libs are in there: cocos2D, cocosDenshion, and kazmath. Ultimately these are the ones that our project will depend on. Now delete that libs folder and send to trash.

* Now we want to cross-reference the cocos2D source, so that it can be managed separately from our App’s code. The way I like to do this now is with git submodules (as I don’t write any code without it being in a git repository, local or remote, as long as there’s some source control!) So for that we go to the command line in the root of our project and type:

git submodule add cocos2d_src

Which will clone that repo into this repo, and put it in the folder called cocos2d_src.

(At this point you may want to create an appropriate .gitignore file for your repo if you haven’t done so already – github provides a decent one for Objective-C or see the bottom of this post for something you can copy-paste)

* Now, in that cocos2d_src (once it’s finished downloading!) folder, drag the cocos2d-ios.xcodeproj file into your project, thereby creating a cross-project reference. Another reason why cross-project references are good is that they compile separately, which means we don’t care if that project is ARC or not, because it gets compiled separately. We want ARC, so it’s good to keep cocos2D source out of our Project’s source, so you don’t have to turn on ARC for half of your project, and off for the other half.  Let them compile separately.

* Now we need to be able to work with that source. As I mentioned, we need to be able to use cocos2D, cocosDenshion, and kazmath. In your PROJECT‘s (You put it in the project’s settings to that the unit tests will also have access to them… does anyone know a ‘cleaner’ way to do this?) build settings, go to Header Search Paths and add 3 entries:

$(SRCROOT)/cocos2d_src/cocos2d                          (recursive)
$(SRCROOT)/cocos2d_src/external/kazmath                 (recursive)
$(SRCROOT)/cocos2d_src/cocosDenshion/cocosDenshion      (recursive)

The next step is to set up the dependencies to this other project. In your Build Phases, add target dependencies to cocos2d, CocosDenshion, and kazmath. In Link Binary With Libaries, add libcocos2d.a, libCocosDenshion.a, and libkazmath.a

Now, in your Project’s Precompiled Header file (Prefix.pch), add #import “cocos2d.h” underneath the UIKit and Foundation framework imports. For those of you a bit new to this, this is a way of saying “make this framework available everywhere, as if I import this in each header file I make”. It’s also a quick check to see if everything has worked so far. Build and run the project and hope it succeeds.

If yes, then great. If not… Oh yeah, you’ll get errors due to ARC being enabled and some code is non-ARC. Either delete the HelloWorldLayer and Intro Layer classes from your project, or remove all lines of code with “release” statements, and remove all dealloc method implementations. Then you also have to update your main.m file as it uses new syntax regarding Autorelease pools:

– replace old non-ARC main.m implementation with ARC main.m implementation:

int main(int argc, char *argv[]) {
  @autoreleasepool {
    return UIApplicationMain(argc, argv, nil, @"AppController");

Otherwise if it still doesn’t work… hmm… double check the steps above? Don’t you just hate those tutorials when it doesn’t work as written?

Now then, this is Checkpoint ONE of the tutorial. Now we have an ARC-ready project that uses Cocos2D source that you can keep up-to-date as it’s checked out from the github repository. Convenient!

Next, onto adding Unit Testing. The next bit was inspired by this post, summarised below:

* Click on your Project to get to its settings -> Add Target -> iOS -> Cocoa Unit Testing Bundle
* Name the new target something appropriate -> “MyAppTests”
* Go to the Build Phases tab of your UNIT TEST target, and create a dependency to your App Target
* Under Build Settings, set Bundle Loader to:


* Set the Test Host to


* Go back to your APP TARGET, and set Symbols Hidden by Default to NO

In addition, you may want to set your UNIT TEST target’s pre-compiled header .pch file to be the same as your App Target, but that’s up to you and your desired configuration.

So, if everything’s gone right, you’re up and running and good to go! You may want to commit this to a repo with a specific tag, so you can check that out in the future and save yourself some time.


You could add ChipmunkPro to your project, if you do it’s pretty straightforward.  Import the folder into your project (with static lib .a file) and add ObjectiveChipmunk.h to your .pch file.  Ooops, but it won’t compile!  You have to link the ImageIO.framework to your project.

EXTRA AWESOME TIP: Use the following as your .gitignore file BEFORE you do any commits!!! Otherwise it’s a pain in the ass for the git beginner to deal with it later, especially when switching branches and dealing with Xcode files…

# Xcode

#Xcode files
#This ignores everything inside a *.xcodeproj except the project.pbxproj

# OSX Noise