如何以编程方式检测Game Center?

zpa*_*ack 4 iphone objective-c gamekit ios4 ios

我正在为游戏添加Game Center支持.因为我的游戏可以在iOS版本上运行回3.0,所以我想让它在没有Game Center的情况下在本地保存成就和排行榜.

现在,我有这个:

+ (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;
}
Run Code Online (Sandbox Code Playgroud)

但是,这似乎根本不起作用.首先,尽管GKLocalPlayer参考声明此类在iOS 4.1及更高版本中可用,但上述测试在iOS 4.0中通过(我没有尝试早期版本).另外,测试还传递了具有iOS 4.1但不支持Game Center的设备(例如,iPhone 3G).

我已经在线浏览了各种GameKit和游戏中心文档,并且没有任何运气可以解决这个问题.我当然可以检测到操作系统版本,但这似乎很蹩脚.对于不支持的硬件(如3G)的情况,这也无济于事.我想也可以检测到这一点,但是再次看起来有点蹩脚.

以编程方式检测Game Center支持的"正确方法"是什么?

小智 11

这是Apple网站上GameKit文档中提供的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这不适用于3G.它返回YES. (4认同)
  • 谢谢,除非他们点击按钮,否则我不想用游戏中心身份验证拨号给用户带来惊喜.除非游戏中心可用,否则我根本不想显示该按钮. (3认同)