Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A little on how to initialize and use iOS's GameCenter

DZone's Guide to

A little on how to initialize and use iOS's GameCenter

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

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.

 

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Published at DZone with permission of David Amador, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}