Under the Bridge

Implementing Game Center

So things are progressing on that October Challenge game project we mentioned before; building on the most excellent LevelSVG cocos2d + Box2D engine, and trawling the Net wildly for clip art and sounds and fonts and convenient helpers to see about getting something striking, if perhaps not appealing precisely, up on the screen despite trolls being too artistically challenged to create assets for a doodle game on our own, never mind anything actually pretty, we’ve got something stumbling towards functionality with zero non-time investment so far other than ponying up for the newly commercial version of Zwoptex (awesome tool, that!). Indeed, it’s to the point where we’ve got scoring mechanics worked out good enough for our extremely low standards of MVP for this project, that being minimal fulfillment of the October Challenge terms, i.e. “at least one person who buys it won’t demand a refund”:


… and there’s enough being tracked there to set up a handful of achievements from; so it’s time today to get onto what the actual point of this effort is. Which, in case you missed that, was to shepherd a Real Live Game Center Using Product through the approval process and into the App Store, so that we get some actual experience sorting that out in program code and in iTunes Connect so we have some kind of clue what we’re doing as soon as a real project that’s needed for drops in our lap. So let’s walk through and take notes as we do that.

First off, SDK-wise Game Center is a part of Game Kit, so visit the mothership and check out:

Game Center Overview

Getting Started With Game Center (PDF)

Game Kit Programming Guide

Game Kit Framework Reference

iTunes Connect Developer Guide (PDF) — read carefully Appendix B on metadata editing restrictions!

GKTapper (sample code)

We’re going to have minimal integration here; a single leaderboard and a handful of achievements, standard UI. All that multiplayer matchmaking stuff and so forth, yeah, we’ll look at that for version 2.0. Maybe.

Soooo, start with the leaderboard. Off to iTunes Connect; enable Game Center for our app; walk through setting up a single ‘Total Score’ leaderboard, even if we hadn’t read the above links the online help would have made it straightforward. Trivial, even.

Now, we need to implement this in the app. A cocos2d app, you may recall from above. It appears that there is a book in progress which has a chapter on exactly that, but it does not seem to be available NOW. Ah well. We did find some relevant links from people who’ve got around to giving it a shot already with cocos2d, and this post mentions some good points too. Fortified with this background, we add a weak linked GameKit.framework to the project, then help ourselves to the GameCenterManager class from the GKTapper sample, making our data model a delegate for one of those created (if Game Kit is available) at launch.

So let’s see how that works. Following recommended practice, we log out of GameCenter.app, remove our app’s non-GameKit-linked build, reboot the device, build and run it aaaand … why yes, there’s a sandbox signin dialog popping up. And everything goes as expected from our reading with it asking us to enter a new name for our account in the sandbox, let’s call it “bztest”; quit and run again and there’s that little “welcome back” display rolling down as expected. Ex-cellent. And we go to Game Center.app, and it shows the “bztest” we entered, and why look, there’s our app with its empty leaderboard and nonexistent achievements! And how do we switch back out of the sandbox to our regular game list, one wonders? Ah, there, just log out and right back in again from Game Center.app, that’s all it takes.

Right then. So account management is pretty much trivial; let’s actually show the boards and submit some scores. Now, a problem other people have run into at this point with their cocos2d projects is not having a UIViewController handy for the default Game Center ingame UI to hang off of; but we built this project off the 0.99.5b3 template, which does create one of those for your projects. Adding the handful of lines provided by the Game Kit documentation to present GKLeaderboardViewController and GKAchievementViewController from it is, well, a handful of lines. And courtesy of that GameCenterManager class we lifted from GKTapper, reporting the score is one line in our game ending handler. Let’s see how that works … perfectly. First try. Woah.

Well, this is really going easier than expected. On to achievements! Back to iTunes Connect we go, and fill in descriptions for the six we’ve come up with for starters, just uploading the app icon to go with them for the moment. Check in the app … yep, our achievement board now lists them as expected. Add in code to set their progress as appropriate, again GKTapper’s GameCenterManager has a one line call for that; and — they work!

So that was astoundingly easy, wasn’t it? Indeed, we may have overprepared quite a bit for the actual difficulty encountered. Mind you, we’re blithely assuming in our code so far that Game Center is always available, the network is always available, and the user always chooses to sign in; plus, we’re unconditionally sending the scores and achievement progress every time they’re earned instead of checking against what’s already been achieved. So there’s a bit of work yet to make it production-ready. But that’s all straightforward enough that we’ll leave that as an exercise for the reader; or you can just wait for whenever Ray Wenderlich gets around to writing his Game Center tutorial, which will no doubt go into his accustomedly exhaustive detail on all those little gotchas.

And as a postscript, we’ll note that there are advantages many people perceive to supporting Game Center via OpenFeint instead of directly, so if trusting your Game Center support to a third party SDK sounds like a wonderful idea to you, check out

Game Center via OpenFeint

for the scoop on that.


And check out Notes on Game Center Integration for some more tips!