3

I suppose this question is language-agnostic, tho I'm asking it in regards to building an iPhone app that uses the new Game Center API, but please feel free to answer in general software engineering terms.

I'm building a game for the iPhone that takes advantage of the new Game Center capabilities (i.e. Auto-matching, leaderboards, achievements, etc.), but I want to write the game so that it works on all iPhones, including those that don't have Game Center installed and cannot make use of the Game Center capabilities. To do this, Apple recommends the approach...

"We'd recommend making one version of the app which dynamically detects whether Game Center is available and uses it (or not) based on that."

With my current level of programming, the simple approach I would take to implementing this would be to check if whether or not Game Center is available and set a simple boolean flag accordingly. Then use that flag to control the flow of execution throughout the software. I'm sure I could make that work, but because I enjoy learning and enjoy programming, I was wondering if there's a better approach or design pattern for disabling blocks of functionality that aren't supported, along with controlling the flow of execution.

Thanks in advance for your wisdom!

4

3 回答 3

2

您通常在这些情况下使用的称为Facade Pattern。在您的情况下,您将为您在应用程序中使用的游戏中心的功能构建一个包装器,然后是两个实现——一个可能只是对游戏中心的代理调用,另一个根据需要返回预设答案.

我会注意到我从来没有做过任何 iOS/objective C 编程,所以我不知道如何在那个环境中正确地实现它。

于 2010-10-22T18:14:46.423 回答
1

查看 Apple 开发网站上的 Game Kit 代码示例。他们实现了一个 GameCenterManager 类,可以很好地完成你想要完成的工作。

于 2010-11-06T17:41:19.690 回答
0

我通常使用扩展 Apple 推荐方法的简单 C 风格函数来测试 Game Center 支持。这增加了对 iPod Touch 第 1 代和 3G 型号的设备测试,因为 Apple 的代码不考虑这些设备。

#import <sys/utsname.h>

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);

    // 1st Gen iPod and 3G don't support Game Center
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *theModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([theModel isEqualToString:@"iPhone1,2"] ||
        [theModel isEqualToString:@"iPod1,1"])
    {
        return FALSE;
    }

    return (gcClass && osVersionSupported);
}

用法很简单

if (isGameCenterAvailable())
{
    // display game center UI
}
于 2010-11-17T00:39:44.523 回答