iPhone Coding Tutorial – Creating an Online Leaderboard For Your Games
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
As you may have seen, there are quite a few services out there offering free leaderboards. These are great and all, but sometimes you want to have complete control over your data. I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games. For those of you who don’t know, a leaderboard is essentially a high scores list. This tutorial will also give you a very simple introduction to interfacing with web services.
The first part of this tutorial will discuss how to build the server. We will be doing this using PHP and MYSQL. I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).
Since this tutorial is so long, I have decided to split it up into pages. You will notice the page number at the bottom. Please use them to navigate between parts of this post. I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying “Where is the rest of the tutorial”.
Skill level: Advanced
Creating The Database
When creating a leaderboard, you will need some way of storing the data. We will be using MYSQL for our storage. You can also be lame and simply use flat files. This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don’t do it.
One thing to note about my php server files. I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP. It’s to show you how to create code that you can connect your iPhone apps to.
I like to create one file that does a complete flash of the database. That way, when I’m testing or switch from staging to production, it is a very simple process. So, with that being said, here is my code for create_db.php.
<?php // create_db.php /** MySQL database name */ define('DB_NAME', ''); /** MySQL database username */ define('DB_USER', ''); /** MySQL database password */ define('DB_PASSWORD', ''); /** MySQL hostname */ define('DB_HOST', $_ENV{DATABASE_SERVER}); $table = "highscores"; // Initialization $conn = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); mysql_select_db(DB_NAME, $conn); // Error checking if(!$conn) { die('Could not connect ' . mysql_error()); } // Drop existing table if exists mysql_query("DROP TABLE $table", $conn); $retval = mysql_query("CREATE TABLE $table( id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, udid VARCHAR(45), name VARCHAR(25), score FLOAT(10,2), date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP )",$conn); if($retval) { echo "Database created..."; } else { echo "Could not create database " . mysql_error(); } mysql_close($conn); ?>
I’m not going to go into too much detail about this code, however I will give a high level description of what’s going on. One thing to note about this code is it assumes you already have the database created.
First, we are defining variables that contain our database information. Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists. This is useful if you want to wipe out your test data. Finally, we create the scores table. Navigate to this file in your browser to run it and create the scores table. Pretty easy right?
You will want to make sure to remove this file from your server after running it to avoid people resetting your database.
Now that our database table has been created, it’s time to implement the web service code to publish new scores to our leaderboard.
Page 1 Page 2 Page 3 Page 4- Posted by brandontreb on 29 Oct 2009 in iPhone Game Programming, iPhone Programming Tutorials
- Digg |
- Del.icio.us |
- Stumble |
- 8 Comments »
8 Responses
RoberRM Says:
October 29th, 2009 at 10:57 am
No, thank YOU very much! ![]()
I was thinking on implementing something like this in my game and you just made my work easier.
Mikko K Says:
October 30th, 2009 at 1:01 am
Hi! Thanks very much for the tutorial!
I have one question about getting highscrores. How I can get the scrores from the php page to the iphone sdk labels and textboxes e.t.c? I don’t want to use UIWebView.
Please answer to the mail
Marin Todorov Says:
October 30th, 2009 at 7:26 am
Very well done ! Love your tuts and I always wonder how to you find time to build them, cz I know how much efforts it takes
For the MySQL,based on my experience I never use FLOAT column type, because I had huge problems with float math on it, I always use DOUBLE instead, which seems to work better.
Also, wouldn’t it be better if the password was sent trough a POST request? Or it was too much code overhead for the simple tutorial ?
Anyhow, I’m always waiting for the new post on your blog, and congrats once more
Marin
brandontreb Says:
October 30th, 2009 at 8:53 am
@Mikko – You need to make your PHP output XML instead of a table. Then parse this XML in your application into an NSDictionary. From there, you can populate whatever you want with this data.
@Marin – I have never had any issues with FLOAT. But I’m sure you could easily switch it out with DOUBLE. As far as teh POST goes, there are many ways security could be improved. HTTP Basic AUTH would be another way to prevent attacks. I didn’t really want to go too much into it in this tutorial as I wanted to keep things simple and show users how to get a leaderboard up with very little code. Thanks for the suggestion.
Walter Says:
November 11th, 2009 at 4:50 am
Thanks for the code. I was wondering about how to make sure the person can reach the website. It seems that Apple has a framework for checking called SCNetworkReachability and some demo code in the SDK. I’m just adding that here to save people the extra google search.
NJDevilfan26 Says:
November 11th, 2009 at 6:31 pm
I cant seem to set up the creat_db.php page I get this error: Could not connect Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2) and also I am getting confused in xcode. Do you think you could post source code to a sample project that would do everything you described in the tutorial?
Buy Vimax Pills Says:
November 19th, 2009 at 12:15 am
Great post. Thanks for this really. I am not a blog reglar blog reader but this blog is truly amazing indeed.


