开发者

How to programmatically detect Game Center?

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 th开发者_C百科is 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜