views:

231

answers:

1

I'm adding Game Center support to my game. Because my game can run on iOS versions back to 3.0, I want to have it fallback to just saving achievements and leaderboards locally in the absence of Game Center.

Right now, I have this:

+ (BOOL) isGameCenterAvailable {
 Class playerClass = NSClassFromString( @"GKLocalPlayer" );
 if( playerClass != nil && [playerClass localPlayer] != nil ) {
  DebugLog( @"Game Center is available" );
  return YES;
 }

 DebugLog( @"Game Center is NOT available" );
 return NO;
}

However, this appears not to work at all. For one thing, despite the GKLocalPlayer reference stating that this class is available in iOS 4.1 and higher, the above test passes in iOS 4.0 (I didn't try earlier versions). For another thing, the test also passes on devices which have iOS 4.1, but which otherwise don't support Game Center (for example, an iPhone 3G).

I've gone through the various GameKit and Game Center docs online, and haven't had any luck figuring this out. I could certainly detect the OS version, but that seems lame. That also wouldn't help for the case of unsupported hardware (like a 3G). That could also be detected I suppose, but again, seems kinda lame.

What's the "right way" to programmatically detect Game Center support?

+9  A: 

This is the code provided in the GameKit documentation on Apple's site:

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";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

    return (gcClass && osVersionSupported);
}

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/GameKit_Guide/GameCenterOverview/GameCenterOverview.html%23//apple_ref/doc/uid/TP40008304-CH5-SW7

swilliams
+1. And wow, Apple, what the heck.
KennyTM
+1 One of the rare cases where version checking is necessary because some private stuff was available earlier.
Eiko
Not sure how I missed that, thanks!
zpasternack
They were planning to release gamecenter in iOS 4.0, but removed it at the last moment... It was even in the Golden Master build of 4.0...
dododedodonl
This doesn't work on the 3G. It returns YES.
PEZ
@PEZ: The code will return YES, but the GC will never authenticate on 3G. You can use the code to check whether to enable GC functionality or not, and when enabled the authentication is required to actually use them. 3G will not get past the last part. :)
MH114
Thanks, what I do not want to surprise my users with a game center authentication dialoge unless they tap a button. And I don't want to show that button at all unless game center is available.
PEZ