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.