Small side-note first, Vizati is now available for the Super Nintendo, more info.
For my third idevblogaday post I’m going to talk about Game Center.
Game Center was released only a couple months ago (September or so) but already tons of applications are using it and even more, players ask for it.
It’s a way to track achievements, leader boards etc. Unfortunately It’s only available from iOS 4.1+, so some compatibility check has to be done.
Let’s start by checking if the device is able to use Game Center
- (BOOL)isGameCenterAvailable { // Check for presence of GKLocalPlayer API. Class gcClass = (NSClassFromString(@"GKLocalPlayer")); // The device must be running running iOS 4.1 or later. NSString *reqSysVer = @"4.1"; // Get Current sys version NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; // Compare both versions BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); // Final Check return (gcClass && osVersionSupported); } |
Now we can use it to check if it’s available before actually using it. I would advise you to make this check only once at start and store it somewhere. Using this function intensively is not recommended.
So to init Game Center:
- (void)initGameCenter { // Only continue if Game Center is available on this device if ([self isGameCenterAvailable]) { // Authenticate the local player [self gameCenterAuthenticate]; // Register the GKPlayerAuthenticationDidChangeNotificationName notification NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver: self selector:@selector(gameCenterAuthenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil]; } } |
Noticed the gameCenterAuthenticate and gameCenterAuthenticationChanged we registered?
First our registered function will check if the player has logged in, if not it will prompt it.
- (void)gameCenterAuthenticate { // Authenticate the local user if([GKLocalPlayer localPlayer].authenticated == NO) { [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { if(error == nil) { // Game Center Active and Player Sucessfully logged in // Store in your global vars that Game Center is Active } else { // Player Is not Logged In, so proceed as not having Game Center // Store in your global vars that Game Center is INACTIVE } }]; } } - (void)gameCenterAuthenticationChanged { [self gameCenterAuthenticate]; // In case the player makes logout we prompt it with login again. } |
To submit something to Game Center:
std::string category = "top_score"; // Identifier you registered for your App with Apple, check iTunesConnect int value = 1000; // Let's say your player made 1000 points NSString *cat = [[NSString alloc] initWithUTF8String:category.c_str()]; // Report the high score to Game Center GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:cat] autorelease]; scoreReporter.value = value; [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { if (error == nil) { NSLog(@"Game Center - High score successfully sent"); } }]; |
And that’s it. There’s tons of other stuff like achievements, online playing etc. But this should get you started.
It was my understanding that in the event information could not be sent to Game Center (no net connection), it was the Apps responsibility to retains all data and send it later.
Is that not true, or is your example just a simplification?
It’s that one thing that has kept me from implementing GameCenter, as it’s a huge headache.
“the Apps responsibility to retains all data and send it later” is in case there is no internet connection present at the moment the player unlocks something. You must save it and send it later when there’s a connection available. Which makes perfect sense.
That code really works, it’s not a simplification. Sending stuff to GameCenter automatically uses the net connection
great job
Thanks for the article. It helped me include the GameCenter feature into my apps.